Anyone know how to call the appropriate deconstructor in this example? Assume Weapon inherits some stuff from Item, but weapon has a few variables of its own as well.
Code:
int main(void)
{
Item *i = new Weapon();
delete i;
}
When I delete i, it only calls the item's deconstructor, and does not call the weapon's deconstructor. I can cast the deletion like this:
Code:
if (i->isWeapon() == TRUE)
{
// isWeapon is a virtual function in Item, returns FALSE in Item,
//and TRUE in Weapon
delete (Weapon*)i;
} else
{
delete i;
}
and then it will call both deconstructors like its supposed to. But in my code I have a ton of places where I just call "delete i", without checking to see if its of Weapon class or Item class. Is there any way to have the Item deconstructor call the weapon deconstructor first, if its a weapon? And not call it if its an Item?