Yahoo

  www.yahoo.com
Work in HR? Unlock Free Profile

Yahoo Technical Yahoo Interview Question

I interviewed in Santa Clara, CA and was asked:
"Why do we need virtual destructor in c++"
Add Tags [?]
Answer

Part of a Technical Yahoo Interview Review - one of 531 Yahoo Interview Reviews

Answers & Comments

0
of 1
vote

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

To comment on this question, Sign In with Facebook or Sign Up

Tags are like keywords that help categorize interview questions that have something in common.