Virtual Destructor C++: If a class has at least one virtual function then class must declare destructor as virtual. This will ensure that destruction happens properly whenever the compiler cleans up the derived objects.
When virtual destructor needed: If a class does not contain any virtual functions, then it may indicate that this class may not supposed to be used as a base class. However, this is no rule to confirm this, but such classes need not have virtual destructor.
In following example, the class vehicle is a Base class but it do not have a virtual destructor, therefore the compiler will not destroy its derived class.
#include <iostream> //main header
using namespace std;//for namespace
class vehicle
{
public:
virtual void drive()
{
cout << "Vehicle drive" << endl;
}
~vehicle() //Destructor is not virtual
{
cout << "Vehicle Destructor " << endl;
}
};
class car : public vehicle
{
public:
~car() // This will not get called
{
cout << "Car Destructor" << endl;
}
};
int main()
{
vehicle *v = new car;
v->drive();
delete v;
return 0;
}
The output is:
Vehicle drive
Vehicle Destructor //Base class destructor called
Virtual keyword with Destructor: If the base class destructor declares itself as virtual but the derived classes do not declare their destructors as virtual, then this will have no impact.
Implicitly, the compiler will treat all the derived class destructors as virtual.
Example,
#include <iostream> //main header
using namespace std; //for namespace
class vehicle
{
public:
virtual void drive()
{
cout << "Vehicle drive" << endl;
}
virtual ~vehicle() //Declared Virtual
{
cout << "Vehicle Destructor " << endl;
}
};
class car : public vehicle
{
public:
~car() //No virtual keyword
{
cout << "Car Destructor" << endl;
}
};
class sportscar : public car
{
public:
~sportscar() //No virtual keyword
{
cout << "Sportscar Destructor" << endl;
}
};
int main()
{
vehicle *v = new sportscar;
v->drive();
delete v;
return 0;
}
Pure Virtual Destructor C++
The declaration of a pure virtual destructor is similar to a pure virtual function, i.e. having =0 in declaration in class. The presence of pure virtual destructor (or pure virtual function) will make the class an abstract class, i.e. the class which cannot be instantiated.
However, unlike pure virtual functions, pure virtual must have body. This is because, the compiler calls the destructor definition in all cases during object destruction (in reverse order of construction).
If the pure virtual destructor do not have body, then linker will complain.
Example,
#include <iostream> //main header
using namespace std; //for namespace
class vehicle
{
public:
virtual void drive()
{
cout << "Vehicle drive" << endl;
}
virtual ~vehicle() = 0; //Pure virtual destructor
//(no definition provided)
};
class car : public vehicle
{
public:
~car()
{
cout << "Car Destructor" << endl;
}
};
int main()
{
vehicle *v = new car;
v->drive();
delete v;
return 0;
}
The linker will throw an error:
The class must provide body of pure virtual destructor to resolve this error. This as shown below:
vehicle::~vehicle() {}