title: Named constructor and inheritance
body: <p>I'm working on C++ framework and would like to apply automatic memory management to a number of core classes. So far, I have the standard approach which is</p>
  
  <pre><code>class Foo 
  {
  
  public:
  
    static
    shared_ptr&lt;Foo&gt; init() 
    {
      return shared_ptr&lt;Foo&gt;(new Foo);
    }
  
    ~Foo() 
    {
    }
  
  protected:
  
    Foo()
    {
    }
  
  };
  
  
  // Example of use
  shared_ptr&lt;Foo&gt; f = Foo::init();
  </code></pre>
  
  <p>However, the above breaks when I subclass Foo, since even tho <code>init()</code> is inherited, it still returns <code>shared_ptr&lt;Foo&gt;</code> which contains a pointer to instance of <code>Foo</code>.</p>
  
  <p>Can anyone think of an elegant solution to this? Should I perhaps just stick with (semi-)manually wrapping instances of class with <code>shared_ptr</code>? This would also give ability to expose parameterized constructors without declaring new named constructors...</p>
  
  <p>Ie.</p>
  
  <pre><code>template &lt;typename T&gt;
  shared_ptr&lt;T&gt; make_shared(T* ptr)
  {
    return shared_ptr&lt;T&gt;(ptr)
  }
  
  // Example
  shared_ptr&lt;T&gt; 
    f1 = make_shared(new Foo()),
    f2 = make_shared(new Foo(1,2));
  </code></pre>