C++ — Should the subclass destructor explicitly call base class destructor? – Education Career Blog

Possible Duplicate:
Do I need to explicitly call the base virtual destructor?

Hello all,

I would like to know whether or not a sub-class destructor should call base-class destructor explicitly. My answer is NO.

For example,

class A
{
public:
   A() {...}
   virtual ~A() {...}
protected:
   ...
private:
   ...

};


class B: public A
{
public:
   B() {...}
   virtual ~B() 
   {
     ...
     // should we call destructor of A?
   }
protected:
   ...
private:
   ...

};

Thank you

,

No, a destructor should never ever be called explicitly (in a subclass or otherwise, pretty much just never), the compiler will take care of that for you.

The only situation where you might want to call it explicitly is where you’re rolling your own memory management, and you’re actually freeing the memory explicitly (rather than deleting a bunch of objects).

,

Your guess is correct.
No need to call base class destructor explicitly.

Leave a Comment