title: Constructor overloading with boost::shared_ptr
body: <p>Consider the following .h file:</p>
  
  <pre><code>#ifndef COM_H_
  #define COM_H_
  
  #include &lt;boost/enable_shared_from_this.hpp&gt;
  #include &lt;boost/shared_ptr.hpp&gt;
  #include &lt;map&gt;
  
  class B;
  
  class A : public boost::enable_shared_from_this&lt;A&gt;{
  public:
      A(){}
      ~A(){}
  
      void Init();
  
      boost::shared_ptr&lt;B&gt; b_ptr_;
  };
  
  class B : public boost::enable_shared_from_this&lt;B&gt;{
  public:
  
      B(){}
      B(boost::shared_ptr&lt;A&gt; a_ptr);
      B(int j, boost::shared_ptr&lt;A&gt; a_ptr);
      ~B(){}
  
      void Init();
      void Init(boost::shared_ptr&lt;A&gt; a_ptr);
      void Init(int j, boost::shared_ptr&lt;A&gt; a_ptr);
  
      std::string b;
      boost::shared_ptr&lt;A&gt; a_ptr_;
  };
  #endif /* COM_H_ */
  </code></pre>
  
  <p>and the .cc file:</p>
  
  <pre><code>#include "com.h"
  
  void A::Init() {
  
      // Case 1 not working
      // boost::shared_ptr&lt;B&gt; b1(new B(shared_from_this()));
      // b1-&gt;Init();
  
      // Case 2 working
      boost::shared_ptr&lt;B&gt; b2(new B());
      b2-&gt;Init(shared_from_this());
  }
  
  B::B(boost::shared_ptr&lt;A&gt; a_ptr) {
      B(2, a_ptr);
  }
  
  B::B(int j, boost::shared_ptr&lt;A&gt; a_ptr) {
      a_ptr_ = a_ptr;
      b = "b";
  }
  
  void B::Init() {
      a_ptr_-&gt;b_ptr_ = shared_from_this();
  }
  
  void B::Init(boost::shared_ptr&lt;A&gt; a_ptr) {
      Init(2, a_ptr);
  }
  
  void B::Init(int j, boost::shared_ptr&lt;A&gt; a_ptr) {
  
      a_ptr_ = a_ptr;
      b = "b";
      a_ptr_-&gt;b_ptr_ = shared_from_this();
  }
  </code></pre>
  
  <p>In main:</p>
  
  <pre><code>#include "com.h"
  #include &lt;iostream&gt;
  int main() {
  
      boost::shared_ptr&lt;A&gt; a(new A());
      a-&gt;Init();
  
      std::cout &lt;&lt; a-&gt;b_ptr_-&gt;b &lt;&lt; std::endl;
  
      return 0;
  }
  </code></pre>
  
  <p>When passing a boost::shared_ptr to a constructor and then calling another (overloaded) constructor with the same pointer as argument, the object pointed by the shared_ptr is lost and the error </p>
  
  <blockquote>
    <p>terminate called after throwing an instance of
    'boost::exception_detail::clone_impl</p>
    
    <blockquote>
      <p>'   what():  tr1::bad_weak_ptr</p>
    </blockquote>
  </blockquote>
  
  <p>is thrown. The same does not happen when calling two overloaded functions (Init) in the same fashion.</p>
  
  <p>Can anyone please explain it?</p>