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.2 required=5.0 tests=BAYES_00,FROM_WORDY, INVALID_MSGID,XPRIO autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,62ea712965950abb,start X-Google-Attributes: gid103376,public From: Troy Noble Subject: Ada tagged types not as powerful as C++ for dispatching??? Date: 1997/08/15 Message-ID: <33F4D31F.6E511B93@mci.com>#1/1 X-Deja-AN: 264449096 To: tmnoble@rmi.net X-Priority: 3 (Normal) Organization: CIM Reply-To: Troy.Noble@mci.com Newsgroups: comp.lang.ada Date: 1997-08-15T00:00:00+00:00 List-Id: Ok, I know Ada95 is very powerful, and there's probably an easy answer to my question. I had to get attention somehow, though... I'm sure it's just 'cause I'm a "newbie" to Ada (but not to OO). My question is related to polymorphism and dynamic dispatching with tagged record types. Hopefully some Ada "black-belt" can show me the errors of my ways. Cohen sure talked me in circles, and now I'm confused. That guy's too smart for me to follow sometimes. Here's what I've got so far. As you can hopefully tell, I've tried to simplify the situation considerably in order to hone in on the real issue. Trust me, I have good reasons for wanting to do what I'm doing. In fact, this works well in every other OO language I've used including C++, Smalltalk, Eiffel, Python, and even Perl (polymorphism & dynamic dispatch are germane to OO, and since Ada is OO... there's got to be some way to do it right in Ada too! I hope.). package Figures is type Figure is tagged record ... -- in true Cohen style, significant detail omitted. end record; type Circle is new Figure with record ... end record; type Figure_Ptr is access all Figure'Class; type Circle_Ptr is access all Figures.Circle; end Figures; Now, in another package I want to dynamically create an instance of a Circle and keep an access variable of type Circle_Ptr (so I can treat it like a Circle sometimes) and also keep an access variable of type Figure_Ptr (so I can dispatch). And the following works just fine: with Figures; procedure Test_Figures is C2_Ptr : Circle_Ptr; A_Figure: Figures.Figure_Ptr; begin A_Figure := new Figures.Circle; -- line (1) C2_Ptr := Circle_Ptr(A_Figure); -- line (2) end; Now the real question is, what syntax do I use if I want to dynamically allocate C2_Ptr FIRST and then have A_Figure be an access type that "points" to the same instance. In other words, reverse the order in which I do lines (1) and (2) above. >From what I've been able to ascertain, this is not nearly as syntactically simple to do, but it is the situation I'm presently faced with. I've tried the following, but it actually creates a COPY of the original circle and then converts the type to Figures.Figure'Class. What I really wanted was to get back just another access to the same instance as C2_Ptr "points to". C2_Ptr := new Figures.Circle; A_Figure := new Figures.Figure'Class'(Figures.Figure'Class (C2_Ptr.all)); -- this creates a new COPY of object?!? Not the desired effect. I've also tried other variations of type conversions, most of which seemed intuitive to me, but the compiler did not like: C2_Ptr := new Figures.Circle; A_Figure := C2_Ptr; -- expected Figure_Ptr, found Circle_Ptr A_Figure := Figures.Figure_Ptr'(C2_Ptr); -- same thing as above A_Figure := Figures.Figure_Ptr(C2_Ptr); -- previous line: operand has deeper accessibility level than target What am I missing? To give another perspective of what I'm trying to do (for those who "know" C++)... I would normally do the following in C++ with great success. class Figure { ... }; class Circle : public Figure { ... }; void someRoutine { Figure *aFigure; Circle *C2_Ptr; C2_Ptr = new Circle; aFigure = C2_Ptr; // Now I can dispatch calls via aFigure, like aFigure->Draw(), // which will invoke Circle's Draw() member function // I can also set values of the Circle, like radius, via C2_Ptr C2_Ptr->radius=1.5; } Hope this makes sense. Thanks in advance for any expert advice. Troy