title: When should we use std::enable_shared_from_this
body: <p>I just knew <code>std::enable_shared_from_this</code> form <a href="http://en.cppreference.com/w/cpp/memory/enable_shared_from_this" rel="noreferrer">this link</a>.<br>
  But after reading the code below, I don't know when to use it.<br></p>
  
  <pre><code>try {
          Good not_so_good;
          std::shared_ptr&lt;Good&gt; gp1 = not_so_good.getptr();
      } catch(std::bad_weak_ptr&amp; e) {
          // undefined behavior (until C++17) and std::bad_weak_ptr thrown (since C++17)
          std::cout &lt;&lt; e.what() &lt;&lt; '\n';    
      }
  </code></pre>
  
  <p>The code above is "not so good" because there is no existing <code>shared_ptr</code> before calling <code>getptr()</code>. So the good thing should be:<br></p>
  
  <pre><code>std::shared_ptr&lt;Good&gt; gp1 = std::make_shared&lt;Good&gt;(); // having a shared_ptr at the beginning
  std::shared_ptr&lt;Good&gt; gp2 = gp1-&gt;getptr();
  </code></pre>
  
  <p>However, if I have already had a <code>shared_ptr</code> object, why don't I just simply code like this: <code>std::shared_ptr&lt;Good&gt; gp2 = gp1;</code>, meaning that I don't need <code>std::enable_shared_from_this</code> at all.<br></p>
  
  <p>In my opinion, using <code>std::enable_shared_from_this</code> is to make sure that more than one <code>shared_ptr</code> objects have the same control block so that we can avoid <a href="https://ideone.com/FlvIWw" rel="noreferrer">the double-delete problem</a>. But if I must remind myself to create a <code>shared_ptr</code> at the beginning, why don't I just remind myself to use <code>shared_ptr</code> object to create a new one, instead of using raw pointer?</p>