You can't delete this .. it makes no sense in my opinion .. your object will get destructed when it goes out of scope ... or as soon as it is not needed anymore if it's an automatic object. Your explode pointer (boom) get's initialized befor the body of Missile's constructor gets executed .. if you assign it a diffrent meaning somewhere along the life time of your missle object (like boom = new explode(..)) .. It is your job to delete boom. If you don't the memory ocupied by boom will be lost to the program forever.
It's important to know how objects get constructed/destructed .. unfortunatly I'm not really up2date on that
A few things that I can remember is that (I hope I'm right)
- global objects get constructed when the program begins and get destructed after main returns
- a local object get's destructed when it goes out of scope for example:
void function SomeFunc()
{
MyClass A; //A gets constructed
{
MyClass B;//Be gets constructed
}//At this point B's destructor is called
return;
//Now A's destructor is called
}
- automatic objects get destructed as soon as they are no longer needed
for example
{
MyClass A; //A's constructor gets called
MyClass B; //B's constructor gets called
MyClass C = A + B; // What happens here is that a temporary (automatic ??!) object gets constructed form A + B then C's copy constructor is called with the temporary object as a parameter .. C is constructed
//The temporary object's destructor gets called
}
// A,B,C's destructors get called (Actually C,B,A in that order)
objects created on the free store (eg. with the new operator) get destructed when the user chooses so for example
void SomeFunc()
{
MyClass * A = new MyClass(); //A's constructor gets called
//do something with A
delete A; //A's destructor is called
}
compared to:
void SomeFunc()
{
MyClass * A = new MyClass(); //A's constructor gets called
//do something with A
}
In the latter case .. because A is a local variable after the function returns we have no more access to it .. since we didn't delete A (wich also calls A's destructor) .. the memory used by A is lost to us forever in this program
Point is Destructors are a means for the programmer to cleanup all memory used by an object
There are a few things I didn't go into .. because I don't remember them that well .. and I'd rather not say silly things (presuming I haven't already)
Cheers