title: Passing pointer to object in constructor
body: <p>I want to create two objects A and B and each object contains each other.</p>
  
  <pre><code>class B;
  
  class A
  {
  public:
      A(B * b) : _b(b) {}
      void printB(void)
      {
          if (0 != _b)
          {
              std::cout &lt;&lt; "B is not null" &lt;&lt; std::endl;
          }
          else
          {
              std::cout &lt;&lt; "B is null" &lt;&lt; std::endl;
          }
      }
  private:
      B * _b;
  };
  
  class B
  {
  public:
      B(A * a) : _a(a) {}
      void printA(void)
      {
          if (0 != _a)
          {
              std::cout &lt;&lt; "A is not null" &lt;&lt; std::endl;
          }
          else
          {
              std::cout &lt;&lt; "A is null" &lt;&lt; std::endl;
          }
      }
  private:
      A * _a;
  };
  
  int main(int argc, char ** argv)
  {
      A * a = 0;
      B * b = 0;
  
      a = new A(b);
      b = new B(a);
  
      a-&gt;printB();
      b-&gt;printA();
  
      delete a;
      delete b;
  
      return 0;
  }
  </code></pre>
  
  <p>As you can see object 'a' contains null pointer 'b'. What is the best way to re-write this code so that 'a' contains a reference to 'b'? (note that object 'a' and 'b' needs to use 'new')</p>
  
  <p>Many thanks!</p>