title: How to enable_shared_from_this of both parent and derived
body: <p>I have simple base and derived class that I want both have <code>shared_from_this()</code>.</p>
  
  <p>This simple solution:</p>
  
  <pre><code>class foo : public enable_shared_from_this&lt;foo&gt; {
      void foo_do_it()
      {
          cout&lt;&lt;"foo::do_it\n";
      }
  public:
      virtual function&lt;void()&gt; get_callback()
      {
          return boost::bind(&amp;foo::foo_do_it,shared_from_this());
      }
      virtual ~foo() {};
  };
  
  class bar1 : public foo , public enable_shared_from_this&lt;bar1&gt; {
      using enable_shared_from_this&lt;bar1&gt;::shared_from_this;
      void bar1_do_it()
      {
          cout&lt;&lt;"foo::do_it\n";
      }
  public:
      virtual function&lt;void()&gt; get_callback()
      {
          return boost::bind(&amp;bar1::bar1_do_it,shared_from_this());
      }
  };
  </code></pre>
  
  <p>Causes exception <code>tr1::bad_weak_ptr</code> in following code:</p>
  
  <pre><code>shared_ptr&lt;foo&gt; ptr(shared_ptr&lt;foo&gt;(new bar1));
  function&lt;void()&gt; f=ptr-&gt;get_callback();
  f();
  </code></pre>
  
  <p>So after "googling" I have found following solution:</p>
  
  <pre><code>class bar2 : public foo {
      void bar2_do_it()
      {
          cout&lt;&lt;"foo::do_it\n";
      }
      shared_ptr&lt;bar2&gt; shared_from_this()
      {
          return boost::static_pointer_cast&lt;bar2&gt;(foo::shared_from_this());
      }
  public:
      virtual function&lt;void()&gt; get_callback()
      {
          return boost::bind(&amp;bar2::bar2_do_it,shared_from_this());
      }
  };
  </code></pre>
  
  <p>And now it works.</p>
  
  <p>Is there any better and more convinient and correct way to <code>enable_shared_from_this</code> for both parent and child?</p>
  
  <p>Thanks</p>