comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Offical Ada FAQ
Date: Fri, 26 Nov 2004 10:01:10 +0100
Date: 2004-11-26T10:01:10+01:00	[thread overview]
Message-ID: <1pc1yfy2p7wqf.bhwh9bkg6kl9.dlg@40tude.net> (raw)
In-Reply-To: sa4llcppofe.fsf@snoopy.apana.org.au

On Fri, 26 Nov 2004 09:13:41 +1100, Brian May wrote:

>>>>>> "David" == David Botton <david@botton.com> writes:
> 
>     David> Strong typing can be carried too far, and some amount of
>     David> implicit conversion is essential to make OOP palatable. But
>     David> note that in Ada 95, even with OOP, we don't allow implicit
>     David> conversions that truncate the extension part of a record
>     David> (this is a relatively common mistake in C++ when passing
>     David> parameters by value).
> 
> I would be curious to know more about this common mistake in C++...

Perhaps David meant:

class A
{
public :
   int Field1;
};

void Foo (A X);

class B : public A
{
public :
   int Field2;
};

void Foo (A X)
{   
   printf ("%i", ((B*) &X)->Field2); // Surprise, surprise
}

int main ()
{
   B  Baz;

   Baz.Field2 = 123;
   Foo (Baz); // Prints garbage or crashes
   return 0;
}

But the above is NOT a mistake. It is absolutely correct behavior! The
actual error is an attempt to cast to B something what is not B. Should Ada
allow by-value parameter passing for some of tagged types (why not), the
effect would be exactly the same: truncating extra members. The only
difference to C++ would be that [view] conversion to B or B'Class would
raise Constraint_Error, while C++ cast swallows anything told.

> Does this work with both "in", "in out" and "out" parameters?
> 
> For out parameters, what happens if the derived type has extra fields
> not in the parent? Is this legal? If it is legal are the extra fields
> defined?

It is just view conversions. The tag of a tagged "out" parameter is set
before the call, so it is legal and safe (up to Constraint_Error).

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



      parent reply	other threads:[~2004-11-26  9:01 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-11-25  4:22 Offical Ada FAQ David Botton
2004-11-25 13:03 ` Rod Chapman
2004-11-25 13:07 ` Ching Bon Lam
2004-11-25 14:05   ` Manuel Collado
2004-11-25 14:44     ` David Botton
2004-11-25 16:45       ` Martin Krischik
2004-11-26 10:24       ` Manuel Collado
2004-11-25 14:33   ` David Botton
2004-11-25 14:31 ` Georg Bauhaus
2004-11-25 16:14   ` Martin Krischik
2004-11-25 17:46     ` David Botton
2004-11-25 17:47   ` David Botton
2004-11-25 20:20 ` Jeffrey Carter
2004-11-25 21:42   ` David Botton
2004-11-25 22:13 ` Brian May
2004-11-26  3:18   ` David Botton
2004-11-26  9:01   ` Dmitry A. Kazakov [this message]
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox