The virtual inheritance and invocation order of destructors

Question | May 7, 2016 | hkumar 

We have following class hierarchy with virtual inheritance.

enter image description here

Here is the source code for above illustration:

class A { 
  public: 
  virtual ~A() { std::cout << "A"; } 
};
class B : virtual public A { 
  public: 
 ~B() { std::cout << "B"; } 
};
class C : virtual public A { 
  public: 
 ~C() { std::cout << "C"; } 
};
class D : public B, public C { 
 public: 
 ~D() { std::cout << "D"; } 
};

Note that:

  1. Both class B and class C virtually inherit class A.
  2. Pay attention to the order in which base classes (B and C) are listed in the declaration of class D.
  3. Destructor in class A is declared virtual.

Suppose we create an instance of class D and delete it:

A* aPtr = new D();
delete aPtr;

In what order the defined destructors would be invoked?

2015 nextptr