Virtual Destructor & Pure Virtual Destructor c++

Share the Article

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:

linker error when pure virtual destructor do not have body

The class must provide body of pure virtual destructor to resolve this error. This as shown below:

vehicle::~vehicle() {}

Main Funda: Pure virtual destructor is only pure virtual function which must have a body

Related Topics:

Parametrized constructor
Virtual Destructor & Pure Virtual Destructor
Smart Pointers: unique_ptr<T>
Diamond problem – Overhead of virtual base
Multiple Inheritance has multiple this pointers
Understanding multiple inheritance & virtual base classes
Understanding the copy constructor
What is move constructor ?
Understanding the order of calling constructors and destructors
What is an explicit constructor ?
Smart Pointers: shared_ptr <T> 
What happens when exception thrown from a constructor?
Why a destructor should never throw exception?
Compiler Generated Destructor is always non-virtual
Which member functions are generated by compiler in class?
Understanding array version of new[] & delete[]

Share the Article

Leave a Reply

Your email address will not be published. Required fields are marked *