Destructor
For any class, a destructor must be explicitly defined, as a virtual function
when derived classes exist. This can be understood by reading the following
lines (for more details, see
Eckel2000,
chapter 15 p665):
int main()
{
// Derived1 a class derived from Base1, a class with no virtual destructor.
Base1 bp = new Derived1;
delete bp; call only the destructor of Base1 => potential bug
// Derived2 a class derived from Base2, a class with a virtual destructor.
Base2 bp2 = new Derived2;
delete bp2; // call the destructor of Derived2 and then the one of Base2
}
In a destructor each pointer member should be deleted (if allocated with new
in constructor) and set to NULL.
To check whether object has been allocated in a constructor, for each pointer
member possibly allocated outside the class, a boolean is set.
Example:
~ObjectClass::ObjectClass
{
if(isObjectMemberIsAllocatedIn) delete ObjectMember;
ObjectMember = NULL;
}
