CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2009
    Posts
    38

    Class Destructor warning

    With the following code I get the warning "warning: 'class Passage' has virtual functions but non-virtual destructor":

    Code:
    class UZZT_Element
    {
        public:
    
        int type;
        int glyph;
        int colour;
    
        virtual void receive() const = 0;
    };
    
    class Passage : public UZZT_Element
    {
        public:
    
        std::string dest_name;
    
        Passage()
        {
            glyph = 0;
            colour = 0;
            type = 11;
        }
    
        void receive() const
        {
    
        }
    };
    I imagine it's to do with the new command hidden away inside the string class. I'm aware that destructors are separate for derived classes unless the base class has the virtual destructor. I just wanted to make sure that I can ignore the compiler warning; my understanding was that the compiler creates destructors as necessary, and that custom destructors are only needed when using the new and delete commands within the class.

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Class Destructor warning

    Base classes should have the destructor virtual. Otherwise one can run into problems destructing the objects due to slicing problems.

    Read this article: http://blogs.msdn.com/oldnewthing/ar...07/127826.aspx.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Feb 2009
    Posts
    326

    Re: Class Destructor warning

    expressed below are my views, correct me if I am wrong:

    a) I don't get a warning while compiling your code (I use gcc)

    b) I don't think a virtual destructor is required for your code from what I can see, in fact I don't even see the need for a destructor.

    c)Like you pointed out, I normally use a destructor only when I want some actions to be performed when the object gets destroyed (like deallocating memory of the dynamically created variables)

    And a virtual destructor is useful when you have base pointer pointing to a derived object to destroy cleanly (like shown below):.

    Code:
    //Assuming the following:
    //BaseClass is the base class
    //DerivedClass is the derived class
    
    DerivedClass dObj;
    BaseClass *bPtr = &dObj;
    
    delete bPtr; //Normally only the destructor of the Base Class is invoked.
    When you have a virtual destructor, the above statement would invoke the destructor of Derived Class, which would later invoke the destructor of the Base Class, thereby doing the job cleanly. In such a situation a virtual destructor is necessary.
    Last edited by Muthuveerappan; August 5th, 2009 at 07:28 AM.

  4. #4
    Join Date
    Aug 2007
    Posts
    858

    Re: Class Destructor warning

    And a virtual destructor is useful when you have base pointer pointing to a derived object to destroy cleanly (like shown below):.
    What you're saying about deleting object polymorphically is correct, but your example has a nasty flaw - you should never call delete on an object that wasn't dynamically allocated with new.

  5. #5
    Join Date
    Feb 2009
    Posts
    326

    Re: Class Destructor warning

    you are right, that would mean disaster, thanks for pointing out.

    I meant the below:
    Code:
    BaseClass *bPtr = new DerivedClass;
    
    delete bPtr; //Normally only the destructor of the Base Class is invoked.

  6. #6
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: Class Destructor warning

    the common advice is destructors in base classes should be public and virtual or protected and non-virtual. Never make a public destructor non-virtual if the class is designed to be used as a base.
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

  7. #7
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    Re: Class Destructor warning

    the compiler warning is a bit misleading, as it complains about the derived class not having a virtual destructor. UZZT_Element should have a virtual d'tor, but no need for Passage to have it.

    I get that same warning in my projects, too, and that's really annoying.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured