title: Should I initialize a shared_ptr inside or outside the class constructor?
body: <p>Given the following example code:</p>
  
  <pre><code>#include &lt;memory&gt;
  
  class Foo {
  public:
      Foo(std::shared_ptr&lt;int&gt; p);
  private:
      std::shared_ptr&lt;int&gt; ptr;
  };
  
  Foo::Foo(std::shared_ptr&lt;int&gt; p) : ptr(std::move(p)) {
  }
  
  class Bar {
  public:
      Bar(int &amp;p);
  private:
      std::shared_ptr&lt;int&gt; ptr;
  };
  
  Bar::Bar(int &amp;p) : ptr(std::make_shared&lt;int&gt;(p)) {
  }
  
  int main() {
      Foo foo(std::make_shared&lt;int&gt;(int(256)));
      Bar bar(*new int(512));
  
      return 0;
  }
  </code></pre>
  
  <p>Both Foo and Bar work correctly. However, are there any differences between creating the shared_ptr when calling the constructor and then transferring ownership with std::move and just passing a reference to the object and delegating the creation of the shared_ptr to the class constructor?</p>
  
  <p>I assume the second way is better since I don't have to move the pointer. However, I've mostly seen the first way being used in the code I'm reading.</p>
  
  <p>Which one should I use and why?</p>