Open In App

When to Use Virtual Destructors in C++?

Last Updated : 25 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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;
}


Output

base 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;
}


Output

base 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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads