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: <m3vhn54plp.fsf@mheaney.ni.net> (raw)
In-Reply-To: 01bdd742$5952e4a0$f330ea9e@ukp03332

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

> Matthew Heaney <matthew_heaney@acm.org> wrote in article 
> > 
> > A downcast from a class-wide type nearer the root to a type (specific or
> > class-wide) more away from the root is perfectly legal in Ada95.  No
> > Unchecked_Conversion is necessary.
> > 
> Don't you lose the extra values associated with the derived class though?
> Maybe I'm wrong, but I thought that's what would happen.

No.  You'd only "lose values" if you chose to view a type as a type
nearer the root.

procedure Op (O : A_Ptr) is 

This is the case here.  If the actual type of O is A2, then you can't
access the A2-specific fields of O, because you're viewing it as a
member of A'Class.
  
The only time data is "lost" is if you make a copy of the object:

procedure Op (O : A_Ptr) is  

   C : constant A := A (O.all);
begin

This is like taking a slice in C++: you get the A-specific part of
object O.all.

That's the reason why I like to use a view conversion instead.  You
always keep all the data, but chose how much of it you like to view.

The declaration

   C : A renames O.all;

gives an A-view of O.

The declaration 

   C : A2 renames A2 (O.all);

gives you an A2-view of O, if O in A2'Class (Constraint_Error
otherwise).




  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 [this message]
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     ` Matthew Heaney
1998-09-06  0:00       ` Samuel Tardieu
1998-09-03  0:00     ` Fergus Henderson
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