When to Use Virtual Destructors in C++?
Last Updated :
25 Jan, 2024
In C++, destructors are special members of a class that frees memory occupied by an object when it goes out of scope. A virtual destructor is a special form of destructor that is declared as virtual in a base class.
In this article, we will discuss the cases when the need for a virtual destructor arises.
When to Use Virtual Destructors?
Virtual destructors in C++ are needed in scenarios in which polymorphism and inheritance are involved, and instances of derived classes are managed pointers to base classes.
If your class has one or more virtual functions that are derived by their child classes and you are using pointers to base class to manage the objects, then you need to implement a virtual destructor so that the relevant version of the destructor is called when the object is deleted.
Example
In the below code, we won't use the virtual constructor and see how the objects are destroyed.
C++
#include <iostream>
using namespace std;
// Base class
class Base {
public:
// Base class constructor
Base() { cout << "base constructor" << endl; }
// Base class destructor
~Base() { cout << "base destructor" << endl; }
};
// Derived class which is publicly inheriting the Base class
class Derived : public Base {
public:
int* ptr;
// Derived class constructor
Derived()
{
ptr = new int[10];
cout << "derived constructor" << endl;
}
// Derived class destructor
~Derived()
{
free ptr;
cout << "derived destructor" << endl;
}
};
int main()
{
// Creating a new Derived object and assigning its
// address to a Base class pointer
Base* ptr = (Base*)new Derived();
// Deleting the created object through the Base class
// pointer. As the Base class destructor is not virtual,
// it will not call the Derived class destructor. This
// can lead to resource leak if the Derived class was
// holding any resources
delete ptr;
return 0;
}
Outputbase constructor
derived constructor
base destructor
Here, only the Base class destructor is called for the Derived class object that is being referred to by the Base class pointer. Due to this, the resources allocated inside the base class are not released leading to memory leaks. In this case, we can make the destructor virtual so that the most derived implementation of it is called.
C++ Program to Show the Use of Virtual Destructor
C++
#include <iostream>
using namespace std;
// Base class
class Base {
public:
// Base class constructor
Base() { cout << "base constructor" << endl; }
// Base class destructor
virtual ~Base() { cout << "base destructor" << endl; }
};
// Derived class which is publicly inheriting the Base class
class Derived : public Base {
public:
int* ptr;
// Derived class constructor
Derived()
{
ptr = new int[10];
cout << "derived constructor" << endl;
}
// Derived class destructor
~Derived()
{
delete ptr;
cout << "derived destructor" << endl;
}
};
int main()
{
// Creating a new Derived object and assigning its
// address to a Base class pointer
Base* ptr = (Base*)new Derived();
// Deleting the created object through the Base class
// pointer. As the Base class destructor is not virtual,
// it will not call the Derived class destructor. This
// can lead to resource leak if the Derived class was
// holding any resources
delete ptr;
return 0;
}
Outputbase constructor
derived constructor
derived destructor
base destructor
If you have the case of multilevel inheritance, the destructor will be called in the order of derivation.
Similar Reads
Move Constructors in C++ A move constructor is a special type of constructor in C++ that is used to create a new object from the already existing object of the same type, but instead of making a copy of it, it makes the new object point to the already existing object in the memory, leaving the source object in a valid but u
10 min read
When to Use Deque Instead of Vector in C++? In C++, both deque and vector are sequence containers that can be used to store collections of elements. However, there are some cases where using deque can be more beneficial than using vector. In this article, we will learn when to use deque instead of vector in C++. When to Prefer Deque Instead o
3 min read
How to Create a Pure Virtual Function in C++? In C++, pure virtual functions are those functions that are not implemented in the base class. They are instead implemented in the derived classes if necessary. In this article, we will discuss how to create a pure virtual function in a class in C++. How to Create a Pure Virtual Function in C++? To
2 min read
Destructor for Dynamic Array in C++ Dynamic memory allocation in C++ allows us to allocate memory during program execution using the pointers. This memory is wholly managed by the programmer from allocation to deallocation. So, when we declare a dynamic member in a class, it is our responsibility to deallocate the memory for that clas
3 min read
How to Define a Move Constructor in C++? In C++, we have a move constructor which is a part of C++11 move semantics and is used to handle resources for temporary and rvalue objects. In this article, we will learn how to write a move constructor in C++. How to Write Move Constructor in C++?The move constructor is defined similarly to a copy
2 min read
How to Call a Virtual Function From a Derived Class in C++? In C++, virtual functions play an important role because they allow the users to perform run-time polymorphism. While dealing with inheritance and virtual functions, it is very crucial to understand how to call a virtual function from a derived class. In this article, we will learn how to call a vir
2 min read