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,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,de1c23707584fc3c X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-04-22 10:40:14 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.fjserv.net!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsfeed.tpinternet.pl!newsfeed.gazeta.pl!news.onet.pl!not-for-mail From: "kat-Zygfryd" <6667@wp.pl> Newsgroups: comp.lang.ada Subject: Re: virtual destructors Date: Tue, 22 Apr 2003 19:40:06 +0200 Organization: news.onet.pl Sender: samael_@op.pl@pc8.konin.cvx.ppp.tpnet.pl Message-ID: References: <1ec946d1.0304220928.72c2acc8@posting.google.com> NNTP-Posting-Host: pc8.konin.cvx.ppp.tpnet.pl X-Trace: news.onet.pl 1051033213 4355 217.99.114.8 (22 Apr 2003 17:40:13 GMT) X-Complaints-To: abuse@onet.pl NNTP-Posting-Date: 22 Apr 2003 17:40:13 GMT X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2600.0000 X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 Xref: archiver1.google.com comp.lang.ada:36360 Date: 2003-04-22T17:40:13+00:00 List-Id: "Matthew Heaney" wrote in message news:1ec946d1.0304220928.72c2acc8@posting.google.com... > "kat-Zygfryd" <6667@wp.pl> wrote in message news:... > > How can I define a virtual destructor in Ada95? > > There are two ways. > > I. Use controlled types > > Have your type (privately) derived from Controlled, and then use > Unchecked_Deallocation to delete it through a class-wide type: > > package P is > > type T is tagged private; > > type T_Class_Access is access all T'Class; > > procedure Op (O : access T); > > private > > type T is new Controlled with record > ...; > end record; > > procedure Finalize (O : in out T); > > end P; > > > package P.C is > > type NT is new T with private; > > private > > type NT is new T with record > ...; > end record; > > procedure Finalize (O : in out NT); > > end P.C; > > procedure Free is > new Ada.Unchecked_Deallocation (T'Class, T_Class_Access); > > declare > O1 : T_Class_Access := new T; > O2 : T_Class_Access := new NT; > begin > Free (O1); --call P.Finalize > Free (O2); --call P.C.Finalize > end; > > > What's happening here is that the Ada run-time system knows that types > in T'Class are controlled, so that when you use > Unchecked_Deallocation, it calls the Finalize operation for the object > being deleted. > > So in this sense destructors are already dispatching in Ada95, and > there's nothing special you need to do. > > Note that Finalize operations for a class are typically implemented by > performing actions specific to the derived type, and then forwarding > the call to the parent type, e.g. > > procedure Finalize (O : in out NT) is > begin > --do NT stuff here > Finalize (T (O)); --"view conversion" to call T's Finalize > end; Thanks, I like that :) Hope it'll work for me Zygfryd