From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,3d313337c39c5dd5 X-Google-Attributes: gid103376,public From: Matthew Heaney Subject: Re: run-time type identification Date: 1998/09/03 Message-ID: #1/1 X-Deja-AN: 387489195 Sender: matt@mheaney.ni.net References: <01bdd72e$49ee66b0$f330ea9e@ukp03332> <01bdd742$5952e4a0$f330ea9e@ukp03332> NNTP-Posting-Date: Thu, 03 Sep 1998 07:28:23 PDT Newsgroups: comp.lang.ada Date: 1998-09-03T00:00:00+00:00 List-Id: "Bob Fletcher" writes: > Matthew Heaney 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).