comp.lang.ada
 help / color / mirror / Atom feed
From: stt@houdini.camb.inmet.com (Tucker Taft)
Subject: Re: run-time type identification
Date: 1998/09/03
Date: 1998-09-03T00:00:00+00:00	[thread overview]
Message-ID: <EyqAnG.L0F.0.-s@inmet.camb.inmet.com> (raw)
In-Reply-To: 01bdd72e$49ee66b0$f330ea9e@ukp03332

Bob Fletcher (bob@radge.globalnet.co.uk) wrote:

: Is there any equivalent in Ada 95 to the C++ "dynamic_cast" operator?

Absolutely (we had it before they did ;-).  Both the membership
test and type conversion were generalized to provide this
capability in a pretty intuitive (and safe) way.  Any text on Ada 95 OOP 
should explain this pretty thoroughly.  RM95 defines this in 4.5.2(3,4,30) 
and 4.6(23,40).

: For example, say you have a class A, and a derived class A2.
: In the package for class A there is also:
: 	type A_Ptr is access A'Class;
: You can assign an access to an object of class A2 to a variable of type
: A_Ptr, but, as far as I know, cannot then de-reference the A_Ptr in such a
: way that the extra bits of class A2 are accessible.

Given X : A_Ptr, you can convert X.all to A2 and get the extra bits.  E.g.:

   A2(X.all).extra_bits.  

You can also convert X to an access-to-A2 and then dereference 
(implicitly or explicitly).  E.g.:

   A2_Ptr(X).all.extra_bits  
 or 
   A2_Ptr(X).extra_bits

Prior to any of these conversions, you might want to find out
whether X designates an object that has all the desired extra bits.
This is done by:

   if X.all in A2'Class then ...

If it doesn't, and you go ahead and do the conversion, you will
get a Constraint_Error due to the failure of the Tag_Check.

: It seems to me that this is something that would make a lot of sense when
: dealing with class-wide access types, which can easily be used to go up the
: object heirarchy, but not, as far as I know, to go back down, (without
: messing about with unchecked_conversion).

See above and RM95 paragraphs 4.5.2(3,4,30) and 4.6(23,42).  
No need for any messy unchecked_conversions. ;-)

: Bob Fletcher
: bob@radge.globalnet.co.uk

--
-Tucker Taft   stt@inmet.com   http://www.inmet.com/~stt/
Intermetrics, Inc.  Burlington, MA  USA
An AverStar Company




      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 ` Steve Sabljak
1998-09-03  0:00   ` Steve Sabljak
1998-09-03  0:00   ` Matthew Heaney
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 ` Samuel Tardieu
1998-09-03  0:00   ` Bob Fletcher
1998-09-03  0:00     ` Fergus Henderson
1998-09-03  0:00     ` Matthew Heaney
1998-09-06  0:00       ` Samuel Tardieu
1998-09-03  0:00 ` Tucker Taft [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