From: Matthew Heaney <matthewjheaney@earthlink.net>
Subject: Re: Classwide Parameter?
Date: Mon, 11 Oct 2004 16:53:32 GMT
Date: 2004-10-11T16:53:32+00:00 [thread overview]
Message-ID: <uvfdhozbl.fsf@earthlink.net> (raw)
In-Reply-To: ckeaod$9hh$03$1@news.t-online.com
matthias_k <nospam@digitalraid.com> 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;
next prev parent reply other threads:[~2004-10-11 16:53 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 [this message]
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
[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