C++ Virtual Destructor
In C++, a destructor is a class member function that is used to free up space or remove an object of the class that has gone out of scope. The name of a destructor is the same as the name of a class's function Object () {[native code]} function, but the destructor uses a tilde (~) symbol before its function name.
What do you mean by Virtual Destructor?
When the derivative class instances are destroyed using a base class pointer object, a virtual destroyer 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 guarantee that both the base and derived class destructors are invoked at run time, but the derived class is called first and subsequently the base class to free up the space used by both destructors.
Why do we utilize Virtual Destructors?
When an object in the class is no longer in space or the main () function is about to exit, the program automatically calls the destructor to free the memory used by the class destructor method. Due to the initial binding of the compiler, only the parent class destroyer is executed while a pointer object of the base class that refers to the derived class is removed. This avoids invoking the destructor of the derived class, which causes memory leaks in the application. When the virtual keyword is followed by the destructor tilde (~) symbol inside the base class, it ensures that the destructor of the derived class is invoked first. The destructor of the base class is then called to free the space taken up by both destructors in the inheritance class.
In C++, here's an example of a program that shows destructor without utilizing virtual destructor:
#include<iostream>
#include<bits/sdtc++.h>
#include<stdlib>
using namespace std;
class Virtual
{
public: /* A public access specifier defines Constructor and Destructor function to call by any object in the class. */
Virtual() // Constructor function.
{
cout<< "\n Constructor Virtual class";
}
~Virtual() // Destructor function
{
cout<< "\n Destructor Virtual class";
}
};
class Derived: public Virtual
{
public: /* A public access specifier defines Constructor and Destructor function to call by any object in the class. */
Derived() // Constructor function
{
cout << "\n Constructor Derived class" ;
}
~Derived() // Destructor function
{
cout << "\n Destructor Derived class" ; /* Destructor function is not called to release its space. */
}
};
int main()
{
Virtual *Vptr = new Derived; // Create a Virtual class pointer object
delete Vptr; /* Here pointer object is called to delete the space occupied by the destructor.*/
}
OUTPUT:
Constructor Virtual class
Constructor Derived class
Destructor Virtual class
……………………………………...
Process exited in 0.7344 seconds with return value 0
Press any key to continue……
Explanation:
When the compiler generates the code, it calls a pointer object in the main method that references to the Virtual class, as seen in the preceding output. As a result, it runs the function Object () {[native code]} () method of the Virtual class before moving on to the function Object () {[native code]} () code of the derived class. The pointer held by the Virtual Class Destroyer and Derived Class Destroyer is then discarded. Virtual class pointers only call. Destroyer of Virtual class not destroyer of derived class. As a result, the program's memory is leaked.
Important: Because the pointer object points to the Virtual class, only the Virtual class destroyer will be called or its occupied space will be removed if the Virtual class destroyer does not use the virtual keyword. Consequently, the derivative class does not call the destroyer to free the memory used by the derivative class, resulting in memory leakage.
In C++, here's an example of a program that shows destructor utilizing virtual destructor:
#include<iostream>
#include<bits/sdtc++.h>
#include<stdlib>
using namespace std;
class Virt
{
public:
Virt() // Constructor member function.
{
cout << "\n Constructor Virtual class"; // It prints first.
}
virtual ~Virt() // Define the virtual destructor function to call the Destructor Derived function.
{
cout << "\n Destructor Base class"; /
}
};
// Inheritance concept
class Derived: public Virt
{
public:
Derived() // Constructor function.
{
cout << "\n Constructor Derived class" ; /* After print the Constructor Virt, now it will prints. */
}
~Derived() // Destructor function
{
cout << "\n Destructor Derived class"; /* The virtual Virt Class? Destructor calls it before calling the Virt Class Destructor. */
}
};
int main()
{
Virt *Vptr = new Derived; // A pointer object reference the Virt class.
delete Vptr; // Delete the pointer object.
}
OUTPUT:
Constructor Virt class
Constructor Derived class
Destructor Derived class
Destructor Virt class
………………………………………..
Process exited in 0.4535 seconds with return value 0
Press any key to exit.
Explanation:
In the program above, we used a virtual destroyer within a base class that calls a derivative class destroyer before calling a base class destroyer to free up space or fix a memory leak.