Thread: Destructor as a virtual

  1. #1
    Registered User Samson144's Avatar
    Join Date
    Jun 2011
    Posts
    5

    Question Destructor as a virtual

    hi.
    How do you is it illegal to define virtual destructors?

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    It is legal.
    Code:
    virtual ~destr();

  3. #3
    Registered User Samson144's Avatar
    Join Date
    Jun 2011
    Posts
    5
    thanks and what about constructors?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Not only it is legal, but it is good practice to declare the destructor of a polymorphic base class as virtual so as to avoid undefined behaviour should an object of a derived class be destroyed through a base class pointer.

    Quote Originally Posted by Samson144
    what about constructors?
    You cannot declare constructors as virtual, but read this FAQ: What is a "virtual constructor"?
    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

  5. #5
    Registered User Samson144's Avatar
    Join Date
    Jun 2011
    Posts
    5
    thanks thanks thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. virtual destructor SYNTAX
    By rahulsk1947 in forum C++ Programming
    Replies: 6
    Last Post: 06-12-2009, 11:26 PM
  2. A virtual destructor dilema
    By tzakieta in forum C++ Programming
    Replies: 10
    Last Post: 09-11-2008, 09:22 AM
  3. virtual destructor
    By George2 in forum C++ Programming
    Replies: 12
    Last Post: 10-18-2007, 12:33 PM
  4. Virtual functions but non-virtual destructor
    By cunnus88 in forum C++ Programming
    Replies: 4
    Last Post: 03-31-2007, 11:08 AM
  5. virtual destructor problem
    By sunnypalsingh in forum C++ Programming
    Replies: 1
    Last Post: 10-15-2005, 05:07 AM