Answer includes what is virtual destructor in C++ with example and use of it or why do we need virtual destructor in C++ object oriented programming. Note that in an interview, if it asked about virtual destructors then don’t forget to answer when to use virtual destructor in C++ class.
Also, note that there is no concept of virtual constructor in C++ programming.
Answer: Virtual destructors maintains the hierarchy of calling destructors from derived to base class.
A class must have a virtual destructor if we hit below criteria.
- Derived class object using a pointer to base class and deleting base class pointer.
For example
Base * p = new Derived (); // up casting
Delete P;
If we don’t make base class destructor virtual, it will not call derived class destructor on above mentioned criteria.
For example
class Base{
public:
~Base(){cout<<"Base class destructor..."<<"\n";} //not virtual destructor
};
class Derived:public Base{
public:
~Derived(){cout<<"Derived Destructor..."<<"\n";}//It won't be called
//as base class destructor is not virtual
};
int main(){
Base* base = new Derived();//upcasting
delete base;
return 0;
}
Output:
Base class destructor…
And, if we make base class destructor virtual then it will call derived class destructor too.
For example
class Base{
public:
//virtual destructor
virtual ~Base(){cout<<"Base class destructor..."<<"\n";}
};
class Derived:public Base{
public:
~Derived(){cout<<"Derived Destructor..."<<"\n";}//It won't be called
//as base class destructor is not virtual
};
int main(){
Base* base = new Derived();//upcasting
delete base;
return 0;
}
Output :
Derived Destructor…
Base class destructor…
NOTE: So for, we understand what virtual destructor is. But we need to extend our answer to why we need virtual destructor in c++ porgramming or what is use of virtual destructor in c++ class?
In the above situations we applied the concept of virtual destructor and maintained the virtual destructor call order from derived to base class.-cheers !!! we got the concept of virtual destructor in c++.
But, what did we achieve here? As a result nothing. So, doesn’t a class in c++ need virtual destructor? Is it useless ?
Definitely not. Assume you wrote a derived class in a very complex software projects and some later point of time, some other developer comes and updates that class with memory de-allocation in it’s destructor. His focus may not upon C++ virtual destructor but on methods , memory allocation and deallocation etc. What will happen then? What will be the issue in the projects? Major issues is, there will be a memory leak in the c++ projects or c++ products. It may be time consuming to resolve issue, find reasons for memory leak et al(consider large project).
As a good design practice we need to write C++ virtual destructor until we are very sure that there is no need to make destructor virtual.
C++ virtual destructor example that avoid memory leak:
class Base{
public:
virtual ~Base(){cout<<"Base class destructor..."<<"\n";}
};
class Derived:public Base{
int *pointer;
public:
Derived()
{
if(pointer != NULL){
pointer = new int[1024];
}//dynamic memory allocation on heap.
}
~Derived(){
cout<<"Derived Destructor..."<<"\n";
delete pointer;
cout <<"memory deleted..."<<"\n";
}
};
int main(){
Base* base = new Derived();//upcasting
delete base;
return 0;
}
Output:
Derived Destructor…
memory deleted…
Base class destructor…
If base class destructor is not virtual it will not call derived class destructor and allocated memory will not be released, resulting memory leak.
NOTES:
- To prevent the deletion of an derived class instance through a base class pointer, we can make the base class destuctor protected and non virtual; this way, compiler won’t allow to call delete on a base class pointer.
- Some misconception is that a class must contain a virtual method to make destructor virtual. This is not true. See the above working examples that don’t contain virtual method. You can consider c++ virtual destructor as a virtual method.
- You may think of a performance issue like we have vtable if we use virtual destructor in c++ etc. As a practice this should not affect performance a lot but saves a lots of time from finding issues & reasons for memory leak. Make a point that finding a memory leak is really a difficult task.