class A {
public:
A() { }
~A() { cout << "A Destructor \n" ; }
};
class B :public A{
public:
B() { }
virtual ~B() { cout << "B Destructor \n" ; }
};
class C : public B {
public:
C() { }
~C() { cout << "C Destructor \n"; }
};
int main()
{
A *pointA = new A;
A *pointB = new B;
A *pointC = new C;
delete pointA;
delete pointB;
delete pointC;
}
class A { public: A() { } ~A() { cout << A Destructor \n
Share
It will invoke undefined behavior at the second (and third) delete, because A’s destructor is not virtual.
§5.3.5/3:
If you make the destructor of A virtual, you get well-defined behavior, and the destructor of the dynamic type is called. (And each of those in turn calls the base destructor.) Your output would be:
For what it’s worth, when you’re that close to a compilable snippet, you should leave the includes. Also, just use
structinstead ofclassto be concise about thepublicstuff, and leave out empty constructors.