Help understanding virtual destructors

i've been learning about virtual methods, and i can grasp the concept pretty easily. But i don't understand the use of virtual destructors,i tried creating a project and experimenting, but i turned out empty handed. My book doesn't seem to explain virtual destructors in depth either . Could anyone help me out on the purpose and use of virtual destructors? Thanks!
Last edited on
They're used in base classes, from which derived classes inherit.
http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.7

Long story short, they're needed for polymorphism, because otherwise the derived class' destructor isn't called.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>

class BaseBad {
public:
	BaseBad() {std::clog << this << " BaseBad created." << std::endl;}
	~BaseBad() {std::clog << this << " BaseBad destroyed." << std::endl;}
};

class DerivedBad: public BaseBad {
public:
	DerivedBad() {std::clog << this << " DerivedBad created." << std::endl;}
	~DerivedBad() {std::clog << this << " DerivedBad destroyed." << std::endl;}
};

class BaseGood {
public:
	BaseGood() {std::clog << this << " BaseGood created." << std::endl;}
	virtual ~BaseGood() {std::clog << this << " BaseGood destroyed." << std::endl;}
};

class DerivedGood: public BaseGood {
public:
	DerivedGood() {std::clog << this << " DerivedGood created." << std::endl;}
	~DerivedGood() {std::clog << this << " DerivedGood destroyed." << std::endl;}
};

int main()
{
	BaseBad *bb = new DerivedBad;
	BaseGood *bg = new DerivedGood;
	std::clog << "\nNotice the lack of \"DerivedBad destroyed.\"\n" << std::endl;
	delete bb;
	delete bg;
}
0x603010 BaseBad created.
0x603010 DerivedBad created.
0x603030 BaseGood created.
0x603030 DerivedGood created.

Notice the lack of "DerivedBad destroyed."

0x603010 BaseBad destroyed.
0x603030 DerivedGood destroyed.
0x603030 BaseGood destroyed.
Consider the following simple example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

struct A
{
   virtual ~A() { std::cout << "A::~A()\n"; }
};

struct B : A { ~B() { std::cout << "B::~B()\n"; };

int main()
{
   {
      A *pa = new B;

      delete pa;
   }
   
   return 0;
}


When the delete is called then it calls in turn the destructor of class A, because the static type of the pointer is A. If there would not be virtual destructor when the destructor for A would be called and the part of the object that belongs to class B would not be destroyed. However when the destructor is virtual the address of the destructor which is in the virtual table is called. This address is the address of the destructor of B. So the destructor for class B is called and after it the destructor of the base class A also is called. In this case the object of type B will be deleted correctly.

That to understand try the same example only remove the reyword virtual for the destructor of A and you will see the difference.
Last edited on
Topic archived. No new replies allowed.