comp.lang.ada
 help / color / mirror / Atom feed
From: Matthew Heaney <matthew_heaney@acm.org>
Subject: Re: run-time type identification
Date: 1998/09/03
Date: 1998-09-03T00:00:00+00:00	[thread overview]
Message-ID: <m33ea965l0.fsf@mheaney.ni.net> (raw)
In-Reply-To: 01bdd73c$082f0230$f330ea9e@ukp03332

"Bob Fletcher" <bob@radge.globalnet.co.uk> writes:

> Yeah, this is the problem, there's no way of ensuring the safety of the
> operation.

No.  If the actual type isn't in A2'Class, then you'll get
Constraint_Error.

> Also, in the code you give, are you certain that any record fields specific
> to the class A2 would be copied by the assignment to the constant? It seems
> likely to me that they would be lost. 

No.

> I imagine that invoking Z.All, in your example, will return only the
> class A part of the object, regardless of whether it is in fact an
> object of class A2.

No.  The type cast to A2 gives you an A2 view of the object.  If O
really is of type A2 (or its descendent), then you'll get all the
fields.

The thing I don't like about Samuel's example is that it makes a copy of
the object:

>    Y : constant A2 := A2 (Z.all);

There is no need for a copy.  Just do a "view conversion":

   Y : A2 renames A2 (Z.all);

or better

   Y : A2'Class renames A2'Class (Z.all);


> As you rightly say, there will be problems if you try to do this in a
> situation where you don't know whether the object in question is of the
> base class or one of it's derived classes, which is what would really be
> useful. The C++ dynamic_cast operator returns a zero (null) pointer if the
> object being pointed to is not of the correct class.

No, there will be _no_ problems.  This is Ada.  If the actual type of
Z.all isn't in A2'Class, then you'll get Constraint_Error.





  parent reply	other threads:[~1998-09-03  0:00 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-09-03  0:00 run-time type identification Bob Fletcher
1998-09-03  0:00 ` Matthew Heaney
1998-09-03  0:00   ` Bob Fletcher
1998-09-03  0:00     ` Matthew Heaney
1998-09-03  0:00 ` Steve Sabljak
1998-09-03  0:00   ` Matthew Heaney
1998-09-03  0:00   ` Steve Sabljak
1998-09-03  0:00 ` Samuel Tardieu
1998-09-03  0:00   ` Bob Fletcher
1998-09-03  0:00     ` Fergus Henderson
1998-09-03  0:00     ` Matthew Heaney [this message]
1998-09-06  0:00       ` Samuel Tardieu
1998-09-03  0:00 ` Tucker Taft
replies disabled

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