🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #2490

Re: Virtual functions and destructors

#2490
From: Alex Martelli
Date: Thu, 09 Nov 1995 19:33
73 lines
2816 bytes
parker@WPI.EDU (Chris Parker) writes:
	...
>Does C++ require you to declare your destructor virtual whenever you declare
>any other member function in the class to be virtual?

I see no words in the draft standard to this effect.

>Is it acceptable according to the standard for the destructor to be the only
>virtual member in the class?

I see nothing in the draft standard forbidding this.

>Is virtualness inherited by destructors?  Does C++ automatically make all 
>applicable superclasses also have virtual destructor if they don't already?

Inheritance flows _downwards_ -- a superclass's characteristics (in
particular, whether its dtor is or isn't virtual) is never changed by
some other class inheriting from it.

>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?

If that subclass is destroyed through a pointer to an ancestor whose
dtor isn't virtual, undefined behaviour (violation of a "shall").
E.g.:

class A {public: virtual ~A()};
class B {public:         ~B()}; // NON-virtual dtor
class C: public A, public B {};

int main()
{
    A* a=new C;	// ok
    B* b=new C;	// also ok
    delete a;	// fine!
    delete b;	// undefined behaviour

    return 0;
}


I _think_ the following is supposed to be fine ("inherited virtualness
of dtor") but I can't find words in the draft standard that SAY so...:

class A {public: virtual ~A();}
class B: public A {};           // no _explicit_ dtor...
class C: public B {};

int main()
{
    A* a=new C;	// 
    delete a;	// this is surely OK, but...
    B* b=new C;	// 
    delete b;	// ...I _think_ this is supposed to be OK too...?

    return 0;
}


Alex
--
DISCLAIMER: these are TOTALLY personal opinions and viewpoints, NOT connected
in any way with my employer, nor any other organization or individual!
Email: martelli@cadlab.it                            Phone: +39 (51) 597313
CAD.LAB s.p.a., v. Ronzani 7/29, Casalecchio, Italia   Fax: +39 (51) 597120

---
[ 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: <martelli.815930692@cadlab>
Path: arc1-ca.pugleaf.net!128.32.155.1!matt
References: <47r71q$cp0@bigboote.WPI.EDU>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!128.32.155.1!matt
From: martelli@cadlab.it (Alex Martelli)
Newsgroups: comp.std.c++
Subject: Re: Virtual functions and destructors
Date: 09 Nov 1995 19:33:00 GMT
Organization: Cad.Lab Spa, Bologna, Italia
Approved: matt@physics2.Berkeley.EDU (Matt Austern)
Message-ID: <martelli.815930692@cadlab>
References: <47r71q$cp0@bigboote.WPI.EDU>
NNTP-Posting-Host: physics2.berkeley.edu