Possible Duplicate:
When to use virtual destructors?
If all the data members of a class (which has virtual function) and it’s inherited class are of non pointer type (means it can not hold any dynamic memoroy), is it required to declare destructor as virtual?
Example
class base {
int x;
public:
virtual void fn(){}
};
class der: public base {
int y;
public:
void fn(){}
};
Here do we need a virtual destructor?
A virtual destructor ensures that the inherited class destructor is called when you have a pointer to a base class.
In this particular case you don’t need it, but a user could inherite from
deranother class (let it befoo) which uses -for example- dynamic memory allocation. In that case the destructor wouldn’t be called unless he has a pointer of typefoo.So no, it’s not “necessary” but if you already have at least a virtual function (hence you already have a VTABLE) there is no harm either. It’s mandatory if you assume that those class are to be inherited by the user and are freed using a pointer to the base class.