comp.lang.ada
 help / color / mirror / Atom feed
From: Simon Wright <simon@pushface.org>
Subject: Re: virtual destructors - doesn't seem to work
Date: 22 Apr 2003 18:32:46 +0100
Date: 2003-04-22T18:32:46+01:00	[thread overview]
Message-ID: <x7vvfx6zbcx.fsf@smaug.pushface.org> (raw)
In-Reply-To: b83u8u$bhj$1@news.onet.pl

"kat-Zygfryd" <6667@wp.pl> writes:

> It doesn't seem to work :(
> Look:
> 
> -- errata to previous code
> type CBase is access all Base'Class;
> type CDerived is access all Derived'Class;
> -- /errata
> 
> procedure Dispose(self: in out CBase) is
> begin
>     Put_Line("CBase.Dispose");
> end Dispose;
> 
> procedure Dispose(self: in out CDerived) is
> begin
>     Put_Line("CDerived.Dispose");
> end Dispose;
> 
> function create_derived return CDerived is
> begin
>     return new Derived;
> end create_derived;
> 
> declare
>     B: CBase := CBase(create_derived);
> begin
>     Dispose(B); -- outputs "CBase.Dispose"
> end;
> 
> what am I doing wrong?

Your Dispose operations aren't dispatching operations (they take a
parameter of a type which you have declared, not an anonymous
access-to-tagged-type).

So when you say Dispose(B); it is Dispose(self: in out CBase) that
gets called.

type T is tagged private;
type T_P is access T;
type T_C is access T'Class;

procedure P1 (P : T);          --  dispatches
procedure P2 (P : access T);   --  dispatches
procedure P3 (P : T_P);        --  doesn't dispatch
procedure P4 (P : T_C);        --  doesn't dispatch


I'm a bit puzzled by what you're trying to do. I would have expected
your procedure Dispose(self: in out CDerived) to need to call your
procedure Dispose(self: in out CBase) (because a Derived is a Base and
the Base part will need finalizing).

I would have thought what you'd really like to do is have just the one
classwide pointer type CBase and have the correct finalization
called. Matt has shown how to do that .. having two classwide pointer
types seems likely to cause trouble.



  reply	other threads:[~2003-04-22 17:32 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-04-22 14:18 virtual destructors kat-Zygfryd
2003-04-22 14:27 ` Stephen Leake
2003-04-22 14:56   ` kat-Zygfryd
2003-04-22 15:45     ` kat-Zygfryd
2003-04-22 16:34       ` tmoran
2003-04-22 21:32         ` Robert A Duff
2003-04-22 17:18       ` Stephen Leake
2003-04-22 16:34         ` Simon Wright
2003-04-22 19:57           ` Stephen Leake
2003-04-22 20:19             ` Simon Wright
2003-04-22 21:23             ` Robert A Duff
2003-04-23 15:16         ` Matthew Heaney
2003-04-22 17:31       ` virtual destructors - doesn't seem to work kat-Zygfryd
2003-04-22 17:32         ` Simon Wright [this message]
2003-04-22 17:33     ` virtual destructors Matthew Heaney
2003-04-22 17:28 ` Matthew Heaney
2003-04-22 17:40   ` kat-Zygfryd
replies disabled

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