title: Factory pattern using unique_ptr in c++
body: <p>I have an old factory implementation in c++, and I want to use unique pointers instead of raw pointers in it. A minimal example of my code is as follows. I have a base class <code>A</code>, and a derived class <code>B</code>. In <code>main()</code>, I pass <code>1</code> to the <code>create</code> method in <code>A</code>, and the type of <code>b1</code> is now changed to <code>B</code>.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;map&gt;
  
  class A {
   public:
    A() {}
    virtual void Foo() {}
    std::map&lt;int, A *&gt; &amp;registerType() {
      static std::map&lt;int, A *&gt; map_instance;
      return map_instance;
    }
    A *create(int n) { return registerType()[n]; }
  };
  
  class B : A {
   public:
    B() { registerType()[1] = this; }
    void Foo() { std::cout &lt;&lt; "I am B!\n"; }
  };
  
  static B b0;
  
  int main() {
    A *b1 = new A();
    b1 = b1-&gt;create(1);
    b1-&gt;Foo();
  
    return 0;
  }
  </code></pre>
  
  <p>Now if I want to change raw pointers to unique pointers, I naturally get a collection of errors (the following code results in errors):</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;map&gt;
  #include &lt;memory&gt;
  
  class A {
   public:
    A() {}
    virtual void Foo() {}
    std::map&lt;int, std::unique_ptr&lt;A&gt;&gt; &amp;registerType() {
      static std::map&lt;int, std::unique_ptr&lt;A&gt;&gt; map_instance;
      return map_instance;
    }
    std::unique_ptr&lt;A&gt; create(int n) { return registerType()[n]; }
  };
  
  class B : A {
   public:
    B() { registerType()[1](this); }
    void Foo() { std::cout &lt;&lt; "I am B too!\n"; }
  };
  
  static B b0;
  
  int main() {
    std::unique_ptr&lt;A&gt; b1(new A());
    b1 = b1-&gt;create(1);
    b1-&gt;Foo();
  
    return 0;
  }
  </code></pre>
  
  <p>The errors are:</p>
  
  <pre><code>In member function 'std::unique_ptr&lt;A&gt; A::create(int)':
  use of deleted function 'std::unique_ptr&lt;_Tp, _Dp&gt;::unique_ptr(const std::unique_ptr&lt;_Tp, _Dp&gt;&amp;) [with _Tp = A; _Dp = std::default_delete&lt;A&gt;]'
     std::unique_ptr&lt;A&gt; create(int n) { return registerType()[n]; }
  
  In constructor 'B::B()':
  no match for call to '(std::map&lt;int, std::unique_ptr&lt;A&gt; &gt;::mapped_type {aka std::unique_ptr&lt;A&gt;}) (B* const)'
     B() { registerType()[1](this); }
                                 ^
  </code></pre>
  
  <p>So I want to know:</p>
  
  <ol>
  <li>Were unique pointers intended to be used in cases like mine? (I assume the response should be yes!)</li>
  <li>I need to pass <code>this</code> as a <code>unique_ptr</code> type to the <code>registerType</code> method. How I can pass the ownership of the pointer to the current instance (<code>this</code> keyword) to a <code>unique_ptr</code>? (If it is possible or was intended to be possible.)</li>
  <li>If it is a good practice to use unique pointers here, how I should implement it?</li>
  </ol>