Short answer:
Make your destructor in Node
virtual.
class Node {
// Stuff...
virtual ~Node();
};
Not-So-Short answer:
When Projectile
and likes of that inherit Node
, data members and functions are inherited from the Node
. The derived class can override or replace the actual function definition in the parent class. This overriding happens at either Compile time or at the run time, depending on weather the function in the base class is declared 'virtual'.
In case of non-virtual destructor, the destructor to be called is determined by the type of pointer. So even though the pointer variable p
is pointing to an instance of Projectile
, delete(p)
would call the destructor defined in Node
.
In case of virtual destructor, the destructor to be called is determined at run-time. So, regardless of the pointer type, the destructor that gets called will be the one that is defined in the object pointed by p
.