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: sabljak@cs.rmit.edu.au (Steve Sabljak) Subject: Re: run-time type identification Date: 1998/09/03 Message-ID: <6sm71d$nee$1@emu.cs.rmit.edu.au>#1/1 X-Deja-AN: 387484395 References: <01bdd72e$49ee66b0$f330ea9e@ukp03332> <6sm537$agc$1@emu.cs.rmit.edu.au> X-Complaints-To: abuse@cs.rmit.edu.au X-Trace: emu.cs.rmit.edu.au 904830829 24014 131.170.24.42 (3 Sep 1998 13:53:49 GMT) Organization: Department of Computer Science, RMIT X-Posting-User: sabljak NNTP-Posting-Date: 3 Sep 1998 13:53:49 GMT Newsgroups: comp.lang.ada Date: 1998-09-03T13:53:49+00:00 List-Id: sabljak@cs.rmit.edu.au (Steve Sabljak) writes: >"Bob Fletcher" writes: >>Is there any equivalent in Ada 95 to the C++ "dynamic_cast" operator? >>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. >>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). >You can do run-time type identification like this >with Ada.Tags; use Ada.Tags; >with A; use A; >procedure Tag_Test is > Generalised : A_Ptr; > Specialised : A2_Ptr; > Specialised_2 : A2_Ptr; >begin > Specialised := new A2'(X => 1, Y => 2); > Generalised := Specialised; > if Generalised'Tag = A2'Tag then > Specialised_2 := A2_Ptr(Generalised); > end if; >end Tag_Test; Or even begin Specialised := new A2'(X => 1, Y => 2); Generalised := Specialised; if Generalised.all in A2 then Specialised_2 := A2_Ptr(Generalised); end if; end Tag_Test; cheers -Steve -- Steve Sabljak - RMIT - 3rd Year Computer Science e-mail: sabljak@cs.rmit.edu.au ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~