-3

Why do we need a virtual destructor with dynamic variables when we have inheritance? and what is the order for destructor execution in both the static/dynamic case? won't the destructor of the most derived class always execute first?

user2949483
  • 103
  • 1
  • 7
  • 5
    You need it so that if deleting a derived type instance via a base class pointer, both the base and derived types' destructors get called. This should be explained in any introductory C++ book. Also, one question at a time please. – juanchopanza Jan 06 '14 at 19:16
  • 1
    You could find detail about the virtual destructor in this blog article : http://www.studytonight.com/cpp/virtual-destructors.php – subhash kumar singh Jan 06 '14 at 19:22

2 Answers2

4

You need a virtual destructor in the base class when you attempt to delete a derived-class object through a base-class pointer.

Case in pont:

class Foo
{
public:
  virtual ~Foo(){};
};

class Bar : public Foo
{
public:
  ~Bar() { std::cout << "Bye-Bye, Bar"; }
};

int main()
{
  Foo* f = new Bar;
  delete f;
}

Without the virtual destructor in the base class, Bar's destructor would not be called here.

John Dibling
  • 94,084
  • 27
  • 171
  • 303
  • "Without the virtual destructor in the base class, Bar's destructor would not be called here." O.K that's what virtual keyword mean, but then when it should be used ? always ? in your example , if Bar;s desctructor is not called then what is the problem? – dicaprio Jan 06 '14 at 19:30
  • 1
    @dicaprio: As a general rule of thumb, you should always have virtual destructors in the base class if the class is going to be derived from. – John Dibling Jan 06 '14 at 19:43
2

Imagine you have this:

class A { 
  public:
  int* x;     
}
class B : public A {
  public:
  int *y;
}


main() {
  A *var = new B;
  delete var;
}

Please assume some constructors destructors. In this case when you delete A. If you have virtual destructors then the B destructor will be called first to free y, then A's to free x.

If it's not virtual it will just call A's destructor leaking y.

The issue is that the compiler can't know beforehand which destructor it should call. Imagine you have a if above that constructs A or B depending on some input and assings it to var. By making it virtual you make the compiler deffer the actual call until runtime. At runtime it will know which is the right destructor based on the vtable.

Sorin
  • 11,270
  • 17
  • 23
  • I think you meant `delete var` not `delete A`. – Zac Howland Jan 06 '14 at 19:24
  • Your example doesn't really explain the problem and best usage of virtual destructors. – dicaprio Jan 06 '14 at 19:26
  • @Sorin that's what I wanted to know, that the compiler calls the destructor of the base class only if there is no virtual. and calls both destructors in case virtual is in thebase class destructor – user2949483 Jan 06 '14 at 19:39
  • This is, of course, _not_ the correct answer. If the destructor isn't virtual, literally anything can happen (and I've see cases where it caused the program to crash, or corrupted memory in other objects). – James Kanze Jan 06 '14 at 20:04