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.
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.
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.
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.
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.
Bookmarks