Article View: comp.std.c++
Article #2528Re: Virtual functions and destructors
From: Herb Sutter
Date: Mon, 13 Nov 1995 06:50
Date: Mon, 13 Nov 1995 06:50
49 lines
2844 bytes
2844 bytes
In article <9531520.16333@mulga.cs.mu.OZ.AU>,
fjh@munta.cs.mu.OZ.AU (Fergus Henderson) wrote:
>>Is virtualness inherited by destructors? Does C++ automatically make all
>>applicable superclasses also have virtual destructor if they don't already?
>
>Yes.
No. :-) (I think he did mean "superclasses" (base classes), it wasn't a typo.
I missed this on first read too.)
[Moderator's note: oops, thanks for correcting me on that one.
The answer to the first question "Is virtualness inherited by
destructors?" is indeed "yes", but as you pointed out, the answer to
the second one is "no", since derived classes (subclasses) inherit
from base classes (superclasses), not vice versa. -fjh]
>>If virtualness of destructors is inherited, then what is supposed to happen
>>to in the case where one of our subclasses uses multiple inheritance and one
>>of the other trees leading to the subclass doesn't declare destructors to be
>>virtual anywhere?
>
>I'm not sure I understand exactly what you are asking.
>Could you be more specific (post code)?
He's talking about 'inheriting up' (making a base's dtor virtual if any
derived dtor is virtual) and so there is no answer because that's not C++.
However, it might help Chris if we answered a related question using
inheritance in the proper direction (down!):
struct Base1 { virtual ~Base1() {}; }
struct Base2 { ~Base2() {}; }
struct Derived : public Base1, public Base2 { ... };
This question is, Is Derived::~Derived() virtual, since one of the bases has a
virtual dtor and one doesn't? This answer is, Yes: 'once virtual, always
virtual' (in derived classes), just like with any virtual member function.
Herb
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Herb Sutter 2228 Urwin, Ste 102 voice (416) 618-0184
Connected Object Solutions Oakville ON Canada L6L 2T2 fax (905) 847-6019
---
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID:
<48541k$58r@steel.interlog.com>Path:
arc1-ca.pugleaf.net!in2.uu.net!bounce-backReferences:
<47r71q$cp0@bigboote.WPI.EDU> <9531520.16333@mulga.cs.mu.OZ.AU>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back Date: 13 Nov 95 06:50:56 GMT Approved: fjh@cs.mu.oz.au From: herbs@interlog.com (Herb Sutter) Newsgroups: comp.std.c++ Subject: Re: Virtual functions and destructors X-Original-Date: Sun, 12 Nov 95 15:33:34 GMT Organization: Connected Object Solutions (div of Daystar Software Systems Design Inc.) Message-ID: <48541k$58r@steel.interlog.com> References: <47r71q$cp0@bigboote.WPI.EDU> <9531520.16333@mulga.cs.mu.OZ.AU> X-Newsreader: News Xpress Version 1.0 Beta #4 X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUBMKbsLOEDnX0m9pzZAQHhCwF+PXZfIB6M9eaXwWNT1+vowKrdRqlnxtZY uKhjwe3KQs2mkMOfKK4pp0tCIHv4d/gw =xUaW