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!newsfeed.stanford.edu!bloom-beacon.mit.edu!npeer.de.kpn-eurorings.net!fu-berlin.de!uni-berlin.de!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: Classwide Parameter? Date: Tue, 12 Oct 2004 09:59:20 +0200 Message-ID: <1ktf7vgrk5pja.uds5ux4wglit.dlg@40tude.net> References: Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de qb39sVBS2jaonWdjSZjfKQZnQUJ3UV+0cY07yEWSDQlKsBPyU= User-Agent: 40tude_Dialog/2.0.12.1 Xref: g2news1.google.com comp.lang.ada:5072 Date: 2004-10-12T09:59:20+02:00 List-Id: On Mon, 11 Oct 2004 19:08:24 +0200, matthias_k wrote: > Thanks for that answer. However, I'm still having problems (I have > rewritten the code to be in one single package now): > > > package Graphics is > > type Shape is abstract tagged null record; > procedure Draw (Obj: Shape'Class); > > type Circle is new Shape with record > Radius: Float; > Center: Float; > end record; > procedure Draw (Obj: Circle); > > type Square is new Shape with record > Size: Float; > end record; > procedure Draw (Obj: Square); > > end; > > > Now, what I want is to have different implementations of the Draw method > for each Subtype. This means that the base should declare Draw as a method. [In Ada terms it should be a primitive operation. ] Class-wide is NOT a primitive operation. When you declare Shape abstract, then what is abstract there? Presumably Draw, which should be overridden (implemented) by each concrete derived type. This all means that Shape.Draw should be at least primitive: procedure Draw (Obj: Shape); Very likely it should also be abstract: procedure Draw (Obj: Shape) is abstract; --- Class-wide subprograms come into consideration only if you want and are able to write something valid for *all* derived types. This something is said to be defined on the class rooted in the given type. For example, Shape'Class is rooted in Shape. Now imagine that your Shape also has a primitive operation Flip, then you can write a class-wide procedure Flip_n_Draw which will be able to Flip and then Draw anything derived from Shape: type Shape is abstract tagged ... -- The methods: procedure Draw (Obj : Shape) is abstract; procedure Flip (Obj : in out Shape) is abstract; -- The class-wides: procedure Flip_n_Draw (Obj : in out Shape'Class); procedure Flip_n_Draw (Obj : in out Shape'Class) is begin Flip (Obj); -- Dispatches to an appropriate Flip Draw (Obj); -- Dispatches to an appropriate Draw end Flip_n_Draw; Because Flip_n_Draw is class-wide it is valid for *any* type derived from Shape. Once you have an object of Circle, Rectangle, whatsoever, you can use Flip_n_Draw with it, without any further coding. The only thing you have to do is to implement the "methods" of Shape. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de