Want a Free Job Posting?

Buy a job posting today and the second one is on us. For a limited time only. Act Now.

Interview Question

Interview Santa Clara, CA

Why do we need virtual destructor in c++


Interview Answer

1 Answer


When we destroy objects of derived classes, the derived class's dtor is called first, then the base class's dtor. But the derived dtor may free/destroy members from the base class. This is a problem when the base class's dtor is called. Making the base class's dtor virtual, prevents the base class dtor from being called, assuming that the derived class has defined it's own dtor. General rule, is if you have a class inherit from another, make the base class virtual.

saltab on Nov 8, 2013

Add Answers or Comments

To comment on this, Sign In or Sign Up.