A difference between a destructor (of course also the constructor) and other member functions is that, if a regular member function has a body at the derived class, only the
Constructor and destructor are different from the rest of regular methods.
Constructor
struct A {};
struct B : A { B() : A() {} };
// but this works as well because compiler inserts call to A():
struct B : A { B() {} };
// however this does not compile:
struct A { A(int x) {} };
struct B : A { B() {} };
// you need:
struct B : A { B() : A(4) {} };
Destructor:
struct C
{
virtual ~C() { cout << __FUNCTION__ << endl; }
};
struct D : C
{
virtual ~D() { cout << __FUNCTION__ << endl; }
};
struct E : D
{
virtual ~E() { cout << __FUNCTION__ << endl; }
};
int main()
{
C * o = new E();
delete o;
}
output:
~E
~D
~C
If the method in base class is marked as virtual
all the inherited methods are virtual as well so even if you don't mark the destructors in D
and E
as virtual
they will still be virtual
and they still get called in the same order.