I'm trying to understand destruction behaviour in C++ by following examples from: https://github.com/peterdemin/virtual-destruction-5-cents
Does that list fullfills every possible flow? What should be added? How given examples can be converted into short terms?
https://stackoverflow.com/a/461224/135079 comes up with "Always make base classes' destructors virtual when they're meant to be manipulated polymorphically." which doesn't cover Scenario 4.
Item 7 in Scott Meyers' Effective C++ states:
- If a class has any virtual function, it should have a virtual destructor;
- Classes not designed to be base classes or not designed to be used polymorphically should not declare virtual destructors.
which is light (should and should not) and confronts with Scenario 2.
UPDATE
I rewritten C++ standard provided by 6502 as pseudo-code:
if static type of object is different from its dynamic type:
if the static type is a base class of the dynamic type:
if the static type has a virtual destructor:
We're fine - dynamic type's destructor will be called
else:
behavior is undefined [1]
else:
behavior is undefined [2]
[1] Code will compile without warnings and probably will work fine, but it's not guaranteed and may cause entangled error at runtime.
[2] That's awkward:
class A {};
class B {};
B *a = (B*)(new A());
delete a;