-
August 5th, 2009, 06:37 AM
#1
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.
-
August 5th, 2009, 07:20 AM
#2
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.
-
August 5th, 2009, 07:25 AM
#3
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.
-
August 5th, 2009, 08:20 AM
#4
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.
-
August 5th, 2009, 08:49 AM
#5
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.
-
August 5th, 2009, 10:07 AM
#6
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.
-
August 5th, 2009, 10:50 AM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|