Like Tree1Likes
  • 1 Post By laserlight

Destructor as a virtual

This is a discussion on Destructor as a virtual within the C++ Programming forums, part of the General Programming Boards category; hi. How do you is it illegal to define virtual destructors?...

  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
    682
    It is legal.
    Code:
    virtual ~destr();
    I never put signature, but I decided to make an exception.

  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
    21,717
    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"?
    Samson144 likes this.
    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

  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

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