2

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

Community
  • 1
  • 1
q0987
  • 34,938
  • 69
  • 242
  • 387

2 Answers2

5

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).

falstro
  • 34,597
  • 9
  • 72
  • 86
3

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

bjskishore123
  • 6,144
  • 9
  • 44
  • 66