virtual destructors

This is a discussion on virtual destructors within the C++ Programming forums, part of the General Programming Boards category; In which of the following scenarios should a destructor be declared virtual in a base class? A. When the destructor ...

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

    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
    Location
    Singapore
    Posts
    20,981
    The answer is D, and to understand why read this FAQ answer: When should my destructor be virtual?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,185
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

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, 10: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21