title: shared_from_this called from constructor
body: <p>I have to register an object in a container upon its creation.
  Without smart pointers I'd use something like this:</p>
  
  <pre><code>a_class::a_class()
  {
      register_somewhere(this);
  }
  </code></pre>
  
  <p>With smart pointers I should use <code>shared_from_this</code> but I can't use that in the constructor.</p>
  
  <p>Is there a <em>clean</em> way to solve this problem? What would you do in a similar situation? 
  I'm thinking about introducing an <code>init</code> method to call just after creation and put everything in a factory function like this:</p>
  
  <pre><code>boost::shared_ptr&lt;a_class&gt; create_a()
  {
      boost::shared_ptr&lt;a_class&gt; ptr(new a_class);
      ptr-&gt;init();
      return ptr;
  }
  </code></pre>
  
  <p>Is it fine or there is a standard procedure to follow in such cases?</p>
  
  <p><strong>EDIT:</strong> Actually my case is more complex. I have 2 object which shall maintain pointers each other. So the truth is I'm not "registering" but creating another object (let's say <code>b_class</code>) which requires <code>this</code> as a parameter. <code>b_class</code> receives <code>this</code> as a weak pointer and stores it. </p>
  
  <p>I'm adding this because since you are giving me design advices (which are very appreciated) at least you can know what I'm doing:</p>
  
  <pre><code>a_class::a_class()
  {
      b = new b_class(this);
  }
  </code></pre>
  
  <p>In my program <code>a_class</code> is an entity and <code>b_class</code> is one of the concrete classes representing the state (in the constructor it's just the starting state). <code>a_class</code> needs a pointer to the current state and <code>b_class</code> needs to manipulate the entity.</p>
  
  <p><code>a_class</code> is responsible for creating and destroying b_class instances and thus maintains a shared_ptr to them but <code>b_class</code> need to manipulate <code>a_class</code> and thus maintains a weak pointer. <code>a_class</code> instance "survives" <code>b_class</code> instances.</p>
  
  <p>Do you suggest to avoid using smart pointers in this case?</p>