Virtual Destructors in C++

  • October 28, 2022
  • C++
Accessing Elements
What is a Virtual Destructor?
  • Deleting a derived class object with a non-virtual destructor using a pointer of the base class type results in undefined behavior.
  • While deleting instances of the derived class using a base class pointer object, a virtual destructor is used to free up the memory space allocated by the derived class object or instance.
  • A virtual keyword is used in a base or parent class destructor to ensure that both the base class and the derived class destructors are called at run time, but it calls the derived class first and then the base class to free up the space occupied by both destructors.
  • NOTE: Constructors can never be Virtual; only Destructors can.
Example: Without virtual destructor

The following program results in undefined behavior. 


#include <iostream>

using namespace std;

class A {
public:
	A()	
	{ 
	    cout << "Constructing A\n"; 
	    
	}
	~A()
	{
	    cout<< "Destructing A\n";
	    }	
};

class B: public A {
public:
	B()	
	{
	    cout << "Constructing B\n"; 
	    
	}
	~B()
	{
	    cout << "Destructing B\n"; 
	    
	}
};

int main()
{
B *d = new B();
A *b = d;
delete b;
getchar();
return 0;
}
Output:
Constructing A
Constructing B
Destructing A
Example: With virtual destructor

Making the base class destructor virtual ensures that the object of the derived class is properly destructed, i.e., both the base class and the derived class destructors are called.


#include <iostream>

using namespace std;

class A {
public:
	A()	
	{ 
	    cout << "Constructing A\n"; 
	    
	}
   virtual	~A()
	{
	    cout<< "Destructing A\n";
	    }	
};

class B: public A {
public:
	B()	
	{
	    cout << "Constructing B\n"; 
	    
	}
	virtual ~B()
	{
	    cout << "Destructing B\n"; 
	    
	}
};

int main()
{
B *d = new B();
A *b = d;
delete b;
getchar();
return 0;
}
Output:
Constructing A
Constructing B
Destructing B
Destructing A
Pure Virtual Destructors:
  • In C++, a pure virtual destructor can be declared.
  • After a destructor is created as a pure virtual object (class instance), the destructor body is provided.
  • This is because destructors are not overridden in derived classes, but are instead called in reverse order. As a result, one must specify a destructor body for a pure virtual destructor.
Example:

#include <iostream>
using namespace std;

class A {
public:
	virtual ~A() = 0;
	// Pure virtual destructor

};
	A::~A() // Explicit destructor call
{
    std::cout << "Pure virtual destructor is called";
}

class B : public A {
public:
	~B() { cout << "~B() is executed"; }
};

int main()
{
	A* b = new B();
	delete b;
	return 0;
}
Output:
~B() is executed
Pure virtual destructor is called

Note: also read about Pure Virtual Functions and Abstract Classes

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Leave a Reply

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