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.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,1c7098170d1fe6a5 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-06-12 13:01:05 PST Path: archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!bloom-beacon.mit.edu!nycmny1-snh1.gtei.net!chcgil2-snf1.gtei.net!news.gtei.net!news.huji.ac.il!not-for-mail From: "Ehud Lamm" Newsgroups: comp.lang.ada Subject: Re: Heterogenous_Array_Test: please explain error Date: Tue, 12 Jun 2001 22:44:22 +0300 Organization: The Hebrew University of Jerusalem Message-ID: <9g5rsc$2h0$1@news.huji.ac.il> References: NNTP-Posting-Host: di4-34.dialin.huji.ac.il X-Trace: news.huji.ac.il 992375502 2592 132.64.14.34 (12 Jun 2001 19:51:42 GMT) X-Complaints-To: abuse@news.huji.ac.il NNTP-Posting-Date: Tue, 12 Jun 2001 19:51:42 +0000 (UTC) X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.00.2014.211 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2014.211 Xref: archiver1.google.com comp.lang.ada:8627 Date: 2001-06-12T22:44:22+03:00 List-Id: The approach is ok. The problem is that you must tell the compiler that the class-wide pointer points to an object of a type that has the spefici component you ask for: since this component is not part of the ancestor type, and tus isn't part of the 'class type. Notice that since Ada tries to check as much as possible before runtime it falgs such a use as problematic, unless you explicitly say that the object is of the specific type (see below). Usually such conversions/castings are not good oop style (use dispatching instrad). Below is a simple example I use to cover these issues, the operations in the package are trivial put statements so I am not including them here. HTH package try_tagged_pack is -- We define an abstract base "class" shape type shape is abstract tagged null record; -- and an abstract procedure that will be dispatcahble procedure draw(s: in out Shape) is abstract; procedure any_shape(s:in out shape'class); procedure any2(s: in out shape); type square is new Shape with record side:integer; end record; procedure draw(s: in out square); type circle is new shape with record radius:integer; end record; procedure draw(c:in out circle); type ptr is access shape'class; type ptr2 is access circle; p:ptr; q:ptr2; s:square; c:circle; -- We can not declare variables of an abstract type. -- sh:shape; -- Nor can we declare a varible of a classwide type (it is unconstraind) -- unless we use initialization to a specific type (like s:string:="ehud") --sh:shape'class; begin draw(s); c.radius:=5; p:= new circle'(radius=>5); q:=new circle; q.radius:=5; -- p is not constrained enough --- p.radius:=5; -- one possible solution (expalin why problematic!): circle(p.all).radius:=5; draw(p.all); -- We invoke any_shape on some variables of different types. any_shape(s); any_shape(c); any_shape(p.all); -- Comapre any_shape and any2 any2(s); any2(c); any2(p.all); end; -- Ehud Lamm mslamm@mscc.huji.ac.il http://purl.oclc.org/NET/ehudlamm <== Me!