Virtual destructor is just a virtual function, so it adheres to the same rules.
When you call delete a
, a destructor is implicitly called. If the destructor is not virtual, you get called a->~A()
, because it's called as every other non-virtual function.
However if the destructor is virtual, you get ~B()
called, as expected: the destructor function is virtual, so what gets called is the destructor of derived class, not base class.
Edit:
Note that the destructor of the base class will be called implicitly after the destructor of the derived class finishes. This is a difference to the usual virtual functions.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…