The following code creates a double pointer B** to a B*. It'll use that pointer to allocate memory for another pointer which will point to a B instance created when start() is called:
class A:
class A
{
public:
A()
{
fb = new B*;
*fb = NULL;
}
~A()
{
if(*fb)
delete *fb;
delete fb;
}
B** getfb()
{
return fb;
}
private:
B** fb;
};
class B:
class B
{
public:
B()
{
B** fb = a->getfb();
*fb = this;
}
~B()
{
B** fb = a->getfb();
delete *fb; // <--- stack overflow
*fb = NULL;
}
private:
A* a;
};
start() (a member function of class C):
void C::start()
{
B** fb = a->getfb(); // 'a' is a pointer to an 'A' instance
if(*fb == NULL)
B* f = new B;
}
So, whenever I call start() and then call ~B(), I get a stack overflow!