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.
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.
Re: Class Destructor warning
Quote:
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.
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.
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.
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.