title: How should you return *this with a shared_ptr?
body: <p>See also: <a href="https://stackoverflow.com/questions/382166/smart-pointers-this-considered-harmful">Similar question</a></p>
  
  <p>The code below is obviously dangerous.  The question is: how do you do keep track of the reference to *this?  </p>
  
  <pre><code>using namespace boost;
  
  // MyClass Definition
  class MyClass {
  
  public:
     shared_ptr&lt; OtherClass &gt; createOtherClass() {
        return shared_ptr&lt; OtherClass &gt; OtherClass( this ); // baaad
     }
     MyClass();
     ~MyClass();
  };
  
  // OtherClass Definition
  class OtherClass {
  
  public:
     OtherClass( const *MyClass myClass );
     ~OtherClass();
  };
  
  // Call; pMyClass refcount = 1
  shared_ptr&lt; MyClass &gt; pMyClass( new MyClass() );
  
  // Call; pMyClass refcount = 1 =&gt; dangerous
  pMyClass-&gt;createOtherClass();
  </code></pre>
  
  <p>I have the answer (posted below), I just want it to be on stackoverflow (where everyone can correct me if I'm wrong.)</p>