class A {
public:
~A() { release(); }
virtual release() { cout << "~A"; }
}
class B : public A {
release() { cout << "~B"; }
}
When I delete B, only A class release() method is called.
What I want to achieve is for every child object, when it is deleted, I want to call release method which is overriden in each child class, without manually specifying destructor for each child with call to release (I'm lazy). Is this actually not possible to achieve in this or any other way?
Destructors are executed in the reverse order of constructors, so when the A
in a B
is destroyed its B
component has already been destroyed and no longer exists. Calling a method on a destroyed object yields undefined behavior, and that's why calls to virtual methods always resolve to the current class type within a constructor or destructor.
So basically no, you cannot do it. You will have to write them all.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With