Re: Classwide Parameter?
From: Martin Krischik (krischik_at_users.sourceforge.net)
Date: 10/12/04
- Next message: Martin Krischik: "Re: Classwide Parameter?"
- Previous message: Martin Krischik: "Re: record extension aggregate for returned type legal?"
- In reply to: matthias_k: "Re: Classwide Parameter?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 12 Oct 2004 10:10:37 +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);
You want an abstract method not a class wide procedure:
procedure Draw (Obj: Shape) is abstract;
>
> 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. However, if I run this program:
>
> <snip>
> with Graphics;
> use Graphics;
>
> procedure Demo is
> type Reference is access all Shape'Class;
> Object: Reference;
> begin
> Object := new Circle;
> Draw( Object.all );
>
> Object := new Square;
> Draw( Object.all );
Only use pointer in Ada if you realy need to:
Test_1:
declare
Object: Shape'Class := Circle'(Radius => 10.0, Center => 10.0);
begin
Draw( Object);
end
Test_2:
declare
Object: Shape'Class := Square'(Size => 10.0);
begin
Draw( Object);
end
> end;
> </snip>
>
> The Shape's Draw method is always called here but it shouldn't. No late
> binding happens. I have tried to make it abstract, but it didn't even
> compile then. What's wrong?
>
> - Matthias
-- mailto://krischik@users.sourceforge.net http://www.ada.krischik.com
- Next message: Martin Krischik: "Re: Classwide Parameter?"
- Previous message: Martin Krischik: "Re: record extension aggregate for returned type legal?"
- In reply to: matthias_k: "Re: Classwide Parameter?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|