You have something like
class B
{
A * a;
}
B * b = new B;
b->a = new A;
If you then call delete b;
, nothing happens to a, and you have a memory leak. Trying to remember to delete b->a;
is not a good solution, but there are a couple of others.
B::~B() {delete a;}
This is a destructor for B that will delete a. (If a is 0, that delete does nothing. If a is not 0 but doesn't point to memory from new, you get heap corruption.)
auto_ptr<A> a;
...
b->a.reset(new A);
This way you don't have a as a pointer, but rather an auto_ptr<> (shared_ptr<> will do as well, or other smart pointers), and it is automatically deleted when b is.
Either of these ways works well, and I've used both.