Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Why would you ever want to use a virtual destructor?

I have been asked this question several times recently while interviewing for jobs. I've never been able to come up with a good answer on the fly. This is partially because I don't really know the answer. I'd like to have a solid answer written down for future interviews.

3 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    Use a virtual destructor in the base class when you create a derived class through a pointer to the base class. That way, when the object is destroyed, it will call both destructors. The reason you should make destructors virtual when they are being inherited from—is because if you don't then, all the destructors might not be called (depending on usage).

    Look at this example code:

    class Person {

    public:

    virtual ~Person() { cout << "Person\n"; }

    };

    class Japanese : public Person

    {

    public:

    ~Japanese() { cout << "Japanese\n"; }

    };

    int main() {

    Person *pPerson = new Japanese;

    delete pPerson;

    //If class Person's destructor wasn't virtual, ~Japanese() would not be called upon destruction.

    return 0;

    }

    ________________________________

    In the above example, the derived class destructor, ~Japanese(), probably won't be called.

    Not having a virtual destructor in your Person class means that the the "delete pPerson;" line in main() yields undefined behaviour, according to the C++ standard. Undefined behaviour means that anything is allowed to happen. A program crash is one of the options within "anything". Another such option is having that crash go away when you change the derived class.

    The practical reason (if you look into the guts of the compiler and how it does things) probably comes down to the fact that virtual functions are often implemented as pointers to functions. Pointers to functions are stored in some class-specific table (and compiler-specific table) table, i.e., an internal data structure. Such a table is often referred to as a virtual function table. Classes without any virtual functions do not have such a table. So, the "undefined behaviour" in this case probably involves some form of invalid pointer operation when attempting to find destructors to call.

  • 1 decade ago

    You MUST declare the destructor virtual ONLY if you are creating a polymorphic class witch has a PUBLIC destructor. That’s it. That’s the only case when you must declare the destructor virtual. If you don’t declare it virtual BAD THINGS will happen (destructors of child classes will not be called when deleting by base class).

    You SHOULD (read MUST) NOT declare your destructor virtual in any other case. If you declare it virtual the biggest problem is someone using your class might think that your class is a polymorphic class that can be inherited from. If this happens the code will probably work but will be awkward and difficult to write and maintain. There is also a performance hit that comes with virtual destructors but unless you are writing something where every nanosecond counts you will not care about this hit.

    Source(s): Article I've wrote (witch contains aditoinal sources) http://www.erata.net/weblog/programming/2006/11/18...
  • Anonymous
    1 decade ago

    The derived class must have allocated memory in its own way specific to its functionality.SO if you have no idea of which derived class you are destroying,so you call the base class destructor and if it is virtual,it actually calls the derived class's destructor.There you can free resources and memory the way you wanted.

    Analogy : You build a house and a farm which are child classes of class building.You need to destroy both now.Suppose the guy who destroys knows nothing about the thing he destroys so he does destruction operations specific to a house like removing the costly items there in the way they are to be removed from a house and then removes the things form a farm as they are to be done for a farm and then the common destruction (using a bulldozer) is performed.Similar to this.I know my analogy sucks,but i hope i communicated what I wanted to say.

Still have questions? Get your answers by asking now.