I came across some odd code like the following:
class B {
int* ab;
///...
~B() { /// not virtual
delete ab;
}
///...
}
class D : public B {
int* ad;
///...
~D() {
delete ab;
delete ad;
}
///...
}
But in my opinion, the destructor of the subclass will definitely call the destructor of its base class. So, I think there is no need for the subclass to deallocate resources allocated by its base class. Thus, there is no need to add delete ab
in the destructor of class D
. What's worse, delete
one object twice is wrong.
However, this code works fine in our systmem for a very long time and passes our whole test cases. Are there any other considerations in this kind of odd implementation?
So I wander, if the destructor of subclass is called, then ,no matter what happened, the destructor of base class will be called later on.
Or, after the execution of subclass's destructor, is there any way to stop the execution of base class's destructor?