title: std::shared_ptr of this
body: <p>I am currently trying to learn how to use smart pointers. However while doing some experiments I discovered the following situation for which I could not find a satifying solution:</p>
  
  <p>Imagine you have an object of class A being parent of an object of class B (the child), but both should know each other:    </p>
  
  <pre><code>class A;
  class B;
  
  class A
  {
  public:
      void addChild(std::shared_ptr&lt;B&gt; child)
      {
          children-&gt;push_back(child);
  
          // How to do pass the pointer correctly?
          // child-&gt;setParent(this);  // wrong
          //                  ^^^^
      }
  
  private:        
      std::list&lt;std::shared_ptr&lt;B&gt;&gt; children;
  };
  
  class B
  {
  public:
      setParent(std::shared_ptr&lt;A&gt; parent)
      {
          this-&gt;parent = parent;
      };
  
  private:
      std::shared_ptr&lt;A&gt; parent;
  };
  </code></pre>
  
  <p>The question is how can an object of class A pass a <code>std::shared_ptr</code> of itself (<code>this</code>) to its child?</p>
  
  <p>There are solutions for Boost shared pointers (<a href="https://stackoverflow.com/questions/142391/getting-a-boostshared-ptr-for-this">Getting a <code>boost::shared_ptr</code> for <code>this</code></a>), but how to handle this using the <code>std::</code> smart pointers?</p>