I am beginning to teach myself C++ and how to handle objects without a garbage collector is causing me some confusion. Here is a simple case of what I am trying to do:
A* a;
B b = new B(a); // Note that B is a derived class of A
a = &b;
while (a != NULL)
{
(*a).run();
}
This all works as I expect it to. Where I am having the issue, is that inside the run() method of B, I want to do something like:
C c = new C(a); // a is the same pointer as above, that has been stored
// and C another derived class from A
a = &c;
and then let run() exit. The while loop in the first block would then call would call run() on the new object. My question is, how do I make sure that the memory from the original b is correctly de-allocated?