Yahoo Interview Question: Why do we need virtual destru... | Glassdoor

Interview Question

Technical Yahoo 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.