comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Classwide Parameter?
Date: Tue, 12 Oct 2004 09:59:20 +0200
Date: 2004-10-12T09:59:20+02:00	[thread overview]
Message-ID: <1ktf7vgrk5pja.uds5ux4wglit.dlg@40tude.net> (raw)
In-Reply-To: ckeelg$dkb$05$1@news.t-online.com

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):
> 
> <snip>
> 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;
> </snip>
> 
> 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



  parent reply	other threads:[~2004-10-12  7:59 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-10-11 16:01 Classwide Parameter? matthias_k
2004-10-11 16:53 ` Matthew Heaney
2004-10-11 17:08   ` matthias_k
2004-10-11 19:16     ` Simon Wright
2004-10-11 22:53     ` Brian May
2004-10-12  2:29     ` Matthew Heaney
2004-10-12  8:01       ` matthias_k
2004-10-12  8:53         ` Martin Krischik
2004-10-12 13:10         ` Matthew Heaney
2004-10-12  7:59     ` Dmitry A. Kazakov [this message]
     [not found]       ` <ckg3h6$qau$03$1@news.t-online.com>
2004-10-12  8:14         ` matthias_k
2004-10-12 15:13           ` Dmitry A. Kazakov
2004-10-12  8:10     ` Martin Krischik
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox