Why is base-class destructor called on derived object when destructor of derived class is non-virtual?

c++, polymorphism, virtual-destructor

Solution

Once `A`'s destructor is declared `virtual`, the destructors of all derived classes are also `virtual`, even if they aren't explicitly declared as such.. So the behaviour you see is exactly what is expected

Problem

Why are all destructors, `~D()`,`~C()`,`~B()`,`~A()` being called in the example below? There is only one virtual destructor: that of `A`. Here is the code: ``` #include<iostream> using namespace std; class A { public: virtual ~A() { cout<<"destruct A\n"; } }; class B:public A { public: ~B() { cout<<"destruct B\n"; } }; class C:public B { public: ~C() { cout<<"destruct C\n"; } }; class D:public C { public: ~D() { cout<<"destruct D\n"; } }; int main() { A* ptr = new D(); delete ptr; return 0; } ```

Original source