c++ - When to use virtual destructors?

ID : 604

viewed : 165

Tags : c++polymorphismshared-ptrvirtual-destructorc++





Top 5 Answer for c++ - When to use virtual destructors?

vote vote

91

Virtual destructors are useful when you might potentially delete an instance of a derived class through a pointer to base class:

class Base  {     // some virtual methods };  class Derived : public Base {     ~Derived()     {         // Do some important cleanup     } }; 

Here, you'll notice that I didn't declare Base's destructor to be virtual. Now, let's have a look at the following snippet:

Base *b = new Derived(); // use b delete b; // Here's the problem! 

Since Base's destructor is not virtual and b is a Base* pointing to a Derived object, delete b has undefined behaviour:

[In delete b], if the static type of the object to be deleted is different from its dynamic type, the static type shall be a base class of the dynamic type of the object to be deleted and the static type shall have a virtual destructor or the behavior is undefined.

In most implementations, the call to the destructor will be resolved like any non-virtual code, meaning that the destructor of the base class will be called but not the one of the derived class, resulting in a resources leak.

To sum up, always make base classes' destructors virtual when they're meant to be manipulated polymorphically.

If you want to prevent the deletion of an instance through a base class pointer, you can make the base class destructor protected and nonvirtual; by doing so, the compiler won't let you call delete on a base class pointer.

You can learn more about virtuality and virtual base class destructor in this article from Herb Sutter.

vote vote

81

A virtual constructor is not possible but virtual destructor is possible. Let us experiment.......

#include <iostream>  using namespace std;  class Base { public:     Base(){         cout << "Base Constructor Called\n";     }     ~Base(){         cout << "Base Destructor called\n";     } };  class Derived1: public Base { public:     Derived1(){         cout << "Derived constructor called\n";     }     ~Derived1(){         cout << "Derived destructor called\n";     } };  int main() {     Base *b = new Derived1();     delete b; } 

The above code output the following:

Base Constructor Called Derived constructor called Base Destructor called 

The construction of derived object follow the construction rule but when we delete the "b" pointer(base pointer) we have found that only the base destructor is called. But this must not happen. To do the appropriate thing, we have to make the base destructor virtual. Now let see what happens in the following:

#include <iostream>  using namespace std;  class Base {  public:     Base(){         cout << "Base Constructor Called\n";     }     virtual ~Base(){         cout << "Base Destructor called\n";     } };  class Derived1: public Base { public:     Derived1(){         cout << "Derived constructor called\n";     }     ~Derived1(){         cout << "Derived destructor called\n";     } };  int main() {     Base *b = new Derived1();     delete b; } 

The output changed as following:

Base Constructor Called Derived Constructor called Derived destructor called Base destructor called 

So the destruction of the base pointer (which takes an allocation on derived object!) follows the destruction rule, i.e first the Derived, then the Base. On the other hand, there is nothing like a virtual constructor.

vote vote

78

Declare destructors virtual in polymorphic base classes. This is Item 7 in Scott Meyers' Effective C++. Meyers goes on to summarize that if a class has any virtual function, it should have a virtual destructor, and that classes not designed to be base classes or not designed to be used polymorphically should not declare virtual destructors.

vote vote

62

Also be aware that deleting a base class pointer when there is no virtual destructor will result in undefined behavior. Something that I learned just recently:

How should overriding delete in C++ behave?

I've been using C++ for years and I still manage to hang myself.

vote vote

57

Make the destructor virtual whenever your class is polymorphic.

Top 3 video Explaining c++ - When to use virtual destructors?







Related QUESTION?