Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should your destructor be virtual? [duplicate]

Possible Duplicate:
When to use virtual destructors?

When should your C++ object's destructor be virtual?

like image 479
Michael Zhou Avatar asked Jul 14 '09 01:07

Michael Zhou


People also ask

When should destructor be virtual?

In particular, here's when you need to make your destructor virtual: if someone will derive from your class, and if someone will say new Derived, where Derived is derived from your class, and if someone will say delete p, where the actual object's type is Derived but the pointer p's type is your class.

Why should the destructor be virtual?

Virtual destructors in C++ are used to avoid memory leaks especially when your class contains unmanaged code, i.e., contains pointers or object handles to files, databases or other external objects. A destructor can be virtual.

What are the necessary conditions for a pure virtual destructor?

It is must to provide a function body for pure virtual destructor as derived class's destructor is called first before the base class destructor, so if we do not provide a function body, it will find out nothing to be called during object destruction and error will occur.

Is it a good practice to declare the base class destructor always virtual?

However, virtual destructors do not make much sense for classes that are not designed to be base classes (a trait that can be enforced with the final identifier since C++11). In general, blindly declaring every destructor as virtual is a bad practice and can potentially lead to a significant waste of resources.


1 Answers

  1. You need virtual destructor when at least one of class methods is virtual.

This is because the reason for virtual method is that you want to use polymorphism. Meaning you will call a method on the base class pointer and you want the most derived implementation - this is the whole point of polymorphism.

Now if you did not have virtual destructor and through the pointer to base class you call destructor you end up calling base class destructor. In this case you want polymorphism to work on your destructor as well, e.g. through calling destructor on your base class you want to end up calling destructor of your most derived class not your base class.

class A {    virtual void f() {}    ~A() {} }  class B : public A {    void f() {}    ~B() {} }  A * thing = new B(); thing->f(); // calls B's f() delete thing; // calls ~A(), not what you wanted, you wanted ~B() 

having ~A() virtual turns on polymorphism

virtual ~A() {} 

So when you now call

delete thing; 

~B() will be called.

You would declare virtual destructors when you design class as an interface e.g. you expect it to be extended or implemented. A good practice in that case is to have a interface class (in the sense of Java interfaces) with virtual methods and virtual destructor and then have concrete implementation classes.

You can see that STL classes don't have virtual destructors so they are not supposed to be extended (e.g. std::vector, std::string ...). If you extend std::vector and you call destructor on base class via pointer or reference you will definitely not call your specialized class destructor which may lead to memory leaks.

like image 77
stefanB Avatar answered Oct 07 '22 15:10

stefanB