Thread: virtual destructors

  1. #1
    Spam is Good
    Join Date
    Jan 2009
    In a cave on Mars

    virtual destructors

    In which of the following scenarios should a destructor be declared virtual in a base class?

    A. When the destructor of the base class will be doing the clean-up of the derived class data
    B. When an implementation for the derived class destructor is not desired
    C. When a derived class allocates system resources which are released in its destructor
    D. When the developer wants to ensure that a derived class destructor is invoked when an instance is destroyed through a base class pointer
    E. When the default constructor of the base class is declared as virtual

    Ok, I feel:
    * A is correct, because destructor are not inherited. And IIRC to call the base class destructor from the derived class, the base class must be virtual.
    * B is incorrect, because we only use virtual if we want the derived class to over right the base class destructor.
    * C is correct, because we want the derived class destructor to more the derived classes memory.
    * D is incorrect, because the derived class destructor is not invoked when the base class is destroyed.
    * E is incorrect, as the constructor is separate from the destructor.

    Can someone please confirm. Thx.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    The answer is D, and to understand why read this FAQ answer: When should my destructor be virtual?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Join Date
    Oct 2007
    Inside my computer
    The idea behind virtual is the polymorphic concept.
    If you create a new instance of a derived class and put it inside a pointer to its base class and call functions through that pointer, the compiler will call the base class's functions, even though a derived function may already exist. This is because the compiler doesn't know the pointer is actually pointing to a derived class instance.
    Now, if you would make that function virtual, the compiler will "look up" the correct type and call the appropriate function, in this case, the derived function if it exists, or the base class's, if it does not.

    The same applies to the destructor.
    The destructor by the compiler when an object is destroyed.

    So with that knowledge in mind, the answers to these questions are:
    A) Incorrect, because the derived constructor will always be called when a derived object is destroyed.
    B) Incorrect, because it makes no sense. Virtual does not "steal" a function call.
    C) Incorrect, because of the same reason as A).
    D) Correct, because if delete is called on a pointer to the base class, ONLY the base class's constructor will be called (since the compiler thinks it's a Base class object the pointer is pointing to). Making it virtual fixes this because of the discussion above.
    E) Incorrect, as you say.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Replies: 2
    Last Post: 06-11-2006, 05:56 PM
  3. Virtual & Pure virtual destructors
    By BMJ in forum C++ Programming
    Replies: 61
    Last Post: 08-22-2002, 09:38 AM
  4. C++ XML Class
    By edwardtisdale in forum C++ Programming
    Replies: 0
    Last Post: 12-10-2001, 11:14 PM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM