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-Thread: 103376,8047848c4805a99e X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news1.google.com!newshub.sdsu.edu!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!elnk-atl-nf1!newsfeed.earthlink.net!stamper.news.atl.earthlink.net!newsread2.news.atl.earthlink.net.POSTED!14bb18d8!not-for-mail Sender: mheaney@MHEANEYX200 Newsgroups: comp.lang.ada Subject: Re: Classwide Parameter? References: From: Matthew Heaney Message-ID: User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Date: Mon, 11 Oct 2004 16:53:32 GMT NNTP-Posting-Host: 165.121.136.56 X-Complaints-To: abuse@earthlink.net X-Trace: newsread2.news.atl.earthlink.net 1097513612 165.121.136.56 (Mon, 11 Oct 2004 09:53:32 PDT) NNTP-Posting-Date: Mon, 11 Oct 2004 09:53:32 PDT Organization: EarthLink Inc. -- http://www.EarthLink.net Xref: g2news1.google.com comp.lang.ada:5049 Date: 2004-10-11T16:53:32+00:00 List-Id: matthias_k writes: > I have experimented a little, and I can't really figure out what I need > classwide parameters for. Class-wide objects dispatch their primitive operations. The call is dynamically bound, not statically bound. > Consider this code: > > package Shape_Pkg is > type Shape is abstract tagged null record; > procedure Draw (obj: Shape); -- try adding "is abstract" here > end; > > package Circle_Pkg is > type Circle is new Shape with null record; > procedure Draw (obj: Circle); > end; > > ... > > procedure Test is > c: Circle; > begin > Draw( c ); -- calls Circle_Pkg.Draw > end Test; > > ------------ > > Now, if I change Shape_Pkg.Draw's formal parameter to Shape'Class, > what's the difference? In both cases it's legal to call Draw on a > Circle. The call to draw in your example above is statically bound. A better way to see what's going on is to do this: procedure Test_Draw (S : Shape'Class) is -- class-wide type begin Draw (S); -- call is dynamically bound end; We can now rewrite your example: procedure Test is C : Circle; begin Test_Draw (C); --will call Circle_Pkg.Draw end; A better example is adding different types that derive from Shape ("are in the Shape class," in Ada-speak): procedure Test is C : Circle; S : Square; T : Triangle; begin Test_Draw (C); --will call Circle_Pkg.Draw Test_Draw (S); --calls Square_Pkg.Draw Test_Draw (T); --calls Triangle_Pkg.Draw end;