C++ cyclic destructors
<p>in file a.h:</p>

<pre><code>class B;
class A
{
public:
    B *b;
    A(B *b = nullptr)
    {
        this-&gt;b = b;
    }
    ~A()
    {
        delete this-&gt;b;
    }
};
</code></pre>

<p>in file b.h:</p>

<pre><code>class A;
class B   
{
public:
    A *a;
    B(A *a = nullptr)
    {
        this-&gt;a = a;
    }
    ~B()
    {
        delete this-&gt;a;
    };
};
</code></pre>

<p>lets pretend that we have a pointer to an A *object, and we want to delete it:</p>

<pre><code>// ...
A *a = new A();
B *b = new B();
A-&gt;b = b;
B-&gt;a = a;
// ...

delete a;

// ...
</code></pre>

<p>A's deconstructor will say delete B; i.e. call B's deconstructor.
B's deconstructor will say delete A.
death loop lé infinitè.</p>

<p>Is there a better way to write code to solve this problem?
This isn't pressing question, just curious.</p>

<p>Thanks!</p>
