title: Is it safe to use the "this" pointer in an initialization list?
body: <p>I have two classes with a parent-child relationship (the <code>Parent</code> class "has-a" <code>Child</code> class), and the <code>Child</code> class has a pointer back to the <code>Parent</code>. It would be nice to initialize the parent pointer upon construction of the child, as follows:</p>
  
  <pre><code>class Child;
  class Parent;
  
  class Child
  {
  public:
   Child (Parent* parent_ptr_) : parent_ptr(parent_ptr_) {};
  
  private:
   Parent* parent_ptr;
  };
  
  class Parent
  {
  public:
      Parent() : child(this) {};
  
  private:
      Child child;
  }
  </code></pre>
  
  <p>Now, I know people recommend not using <code>this</code> in initialization list, and <a href="http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.6" rel="noreferrer">C++ FAQ</a> says I'm gonna get a compiler warning (BTW, on VS2010, I don't get a warning), but I really like this better then calling some set function in <code>Parent</code>'s constructor. My questions are:</p>
  
  <ul>
  <li>Is the parent <code>this</code> pointer well-defined when the <code>Child</code> object is being created?</li>
  <li>If so, why is it considered bad practice to use it as above?</li>
  </ul>
  
  <p>Thanks,</p>
  
  <p>Boaz</p>
  
  <p><strong>EDIT:</strong> Thanks Timbo, it is indeed a <a href="https://stackoverflow.com/questions/4006160/in-c-initialize-a-class-member-with-this-pointer-during-construction">duplicate</a> (huh, I even chose the same class names). So lets get some added value: how about references? Is it possible / safe to do the following? :</p>
  
  <pre><code>class Child
  {
  public:
   Child (Parent&amp; parnet_ptr_) : parent_ptr(parent_ptr_) {};
  
  private:
   Parent* parent_ptr;
  };
  
  class Parent
  {
  public:
      Parent() : child(*this) {};
  
  private:
      Child child;
  }
  </code></pre>