Why should a base class destructor usually be virtual when objects are deleted through base pointers?
Correct answer
B. So the derived destructor also runs during delete
18 views
B. So the derived destructor also runs during delete
18 views
Deleting through a base pointer with a non-virtual destructor is undefined behaviour and typically skips the derived destructor, leaking whatever the derived class allocated. A virtual destructor makes delete dispatch to the most-derived destructor and unwind the chain correctly. A useful rule is that any class with at least one virtual function should also declare its destructor virtual.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is B? Ask here and get an explanation.