c-plus-plus
  1. c-plus-plus-virtual-destructor-in-c

Virtual Destructor in C++

In C++, a virtual destructor is a special type of destructor that is declared with the virtual keyword. It is used to ensure proper destruction of derived class objects when they are destroyed through a pointer to a base class.

Syntax

class Base {
public:
    virtual ~Base() {
        // code
    }
};

class Derived : public Base {
public:
    ~Derived() {
        // code
    }
};

In the above example, the destructor of the Base class is declared as virtual, while the destructor of the Derived class is non-virtual.

Example

#include <iostream>
using namespace std;

class Base {
public:
    virtual ~Base() {
        cout << "Base destructor called" << endl;
    }
};

class Derived : public Base {
public:
    ~Derived() {
        cout << "Derived destructor called" << endl;
    }
};

int main() {
    Base* ptr = new Derived();
    delete ptr;
    return 0;
}

Output

Derived destructor called
Base destructor called

Explanation

In the above example, we have two classes Base and Derived, where Derived is derived from Base. We have declared the destructor of the Base class as virtual and the destructor of the Derived class as non-virtual.

In the main function, we create a pointer of type Base that points to an object of the Derived class. When we call delete on this pointer, the destructor of the Derived class is called first, followed by the destructor of the Base class.

Use

Virtual destructors are used in object-oriented programming to ensure that the destructor of the most derived class is called when an object is destroyed through a pointer of a base class.

If we don't declare the destructor of the base class as virtual, only the destructor of the base class will be called when an object of a derived class is destroyed through a pointer to a base class. This can lead to memory leaks and other problems.

#include <iostream>
using namespace std;

class Base {
public:
    ~Base() {
        cout << "Base destructor called" << endl;
    }
};

class Derived : public Base {
public:
    ~Derived() {
        cout << "Derived destructor called" << endl;
    }
};

int main() {
    Base* ptr = new Derived();
    delete ptr; // only calls Base destructor
    return 0;
}

In the above example, we have the same classes as before, but the destructor of the Base class is now non-virtual. When we call delete on the pointer to the derived class object, only the destructor of the Base class is called, resulting in a memory leak for any resources held by the derived class object.

Important Points

  • A virtual destructor is declared with the virtual keyword.
  • A virtual destructor ensures proper destruction of derived class objects when they are destroyed through a pointer to a base class.
  • If the base class destructor is not declared as virtual, only the destructor of the base class will be called when an object of a derived class is destroyed through a pointer to a base class.
  • It is a good coding practice to declare the destructor of any base class in a hierarchy as virtual.

Summary

In summary, virtual destructors are used to ensure proper destruction of objects in object-oriented programming, particularly when they are destroyed through a pointer to a base class. It is good coding practice to declare the destructor of any base class in a hierarchy as virtual, to avoid memory leaks and other problems.

Published on: