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,7a180be12347b9d3 X-Google-Attributes: gid103376,public X-Google-Thread: 1108a1,7a180be12347b9d3 X-Google-Attributes: gid1108a1,public X-Google-ArrivalTime: 2002-02-07 07:03:07 PST Path: archiver1.google.com!news1.google.com!sn-xit-02!supernews.com!news.tele.dk!small.news.tele.dk!193.190.198.17!newsfeeds.belnet.be!news.belnet.be!news2.euro.net!uunet!ash.uu.net!spool0900.news.uu.net!reader0900.news.uu.net!not-for-mail From: "Hyman Rosen" Newsgroups: comp.lang.ada,comp.object References: <3c62524f.93369796@News.CIS.DFN.DE> Subject: Re: Merits of re-dispatching [LONG] Date: Thu, 7 Feb 2002 10:03:44 -0500 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 Organization: KBC Financial Products Message-ID: <1013094178.985786@master.nyc.kbcfp.com> Cache-Post-Path: master.nyc.kbcfp.com!unknown@mosquito.nyc.kbcfp.com X-Cache: nntpcache 2.3.3 (see http://www.nntpcache.org/) NNTP-Posting-Host: 204.253.250.10 X-Trace: 1013094183 reader0.ash.ops.us.uu.net 29775 204.253.250.10 Xref: archiver1.google.com comp.lang.ada:19706 comp.object:33560 Date: 2002-02-07T10:03:44-05:00 List-Id: "Dmitry A. Kazakov" wrote in message news:3c62524f.93369796@News.CIS.DFN.DE... > C++ and Ada 95 support re-dispatching, which I think is inherently unsafe. I don't know about Ada, but there is nothing unsafe about C++'s "redispatching". > Consider the following: > > type A is new Ada.Finalization.Limited_Controlled with ... > procedure Foo (Object : A); > procedure Finalize (Object : in out A) ; > > Now let AA derived from A override Foo and Finalize: > > type AA is new A with record > ... -- Some components, which may use components of A > end record; > procedure Foo (Object : AA) ; > procedure Finalize (Object : in out AA) is > begin > ... -- Finalize components, maybe accessing components of A > Finalize (A (Object)); -- Finalize parent > end Finalize; In C++, Foo(Object:AA) is *not* an overrider of Foo(Object:A), just another function that happens to share the same name. If you want to override Foo in AA, the parameter type must remain A. > Consider the following implementation of A.Finalize: > > procedure Finalize (Object : in out A) is > begin > Foo (A'Class (Object)); -- Re-dispatch to the child's Foo > end Finalize; Again, this is false for C++. In a C++ constructor or destructor, the type of the object is the type of the class of which the constructor or destructor is a member. So in the C++ equivalent of Finalize(A), the call to Foo(A'Class) would call Foo(A), not Foo(AA), because in the destructor of A, the object is an A, not an AA, even when we are destructing the A component of an AA. This is good, since, as you point out, the AA part is no longer there! > Actually it is a common error among fresh baked C++ programmers > to call virtual functions (dispatch) from constructors. True, but the error is that (from their point of view) the wrong function gets called, not that the right function gets called on destructed pieces of objects. I guess this is another entry for my "why C++ is better than Ada" file :-) (Or maybe it's the first? :-)