title: Smart pointers in prototype pattern C++
body: <p>I'm currently looking at some code i have found on github, however I'm quite confused about some sections of the code and what it does.</p>
  
  <p>What does the line below mean? Key is a <code>bulletType</code>, value is a smart pointer, what is <code>hash&lt;int&gt;</code>?</p>
  
  <pre><code>unordered_map&lt;BulletType, unique_ptr&lt;Bullet&gt;, hash&lt;int&gt; &gt; m_Bullets
  </code></pre>
  
  <p>What does the below code do? </p>
  
  <p>Does it create a new object of <code>GoodBullet</code> and return an address to it with <code>*this</code> as argument to the constructor?</p>
  
  <pre><code>return std::make_unique&lt;GoodBullet&gt;(*this)
  </code></pre>
  
  <p>Main code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;unordered_map&gt;
  #include &lt;string&gt;
  #include &lt;memory&gt;
  using namespace std;
  
  
  /** Bullet is the base Prototype */
  class Bullet
  {
  protected:
      string _bulletName;
      float _speed;
      float _firePower;
      float _damagePower;
      float _direction;
  
  public:
      Bullet(){}
      Bullet(string bulletName, float speed, float firePower, float damagePower) 
      : _bulletName(bulletName), _speed(speed), _firePower(firePower), _damagePower(damagePower) 
      {}
      virtual ~Bullet() {}
      virtual unique_ptr&lt;Bullet&gt; clone() = 0;
      void fire(float direction)
      {
          _direction = direction;
  
          cout &lt;&lt; "Name        : " &lt;&lt; _bulletName &lt;&lt; endl
               &lt;&lt; "Speed       : " &lt;&lt; _speed &lt;&lt; endl
               &lt;&lt; "FirePower   : " &lt;&lt; _firePower &lt;&lt; endl
               &lt;&lt; "DamagePower : " &lt;&lt; _damagePower &lt;&lt; endl 
               &lt;&lt; "Direction   : " &lt;&lt; _direction &lt;&lt; endl &lt;&lt; endl;
      }
  };
  
  /** SimpleBullet is a Concrete Prototype */
  class SimpleBullet : public Bullet
  {
  
  public:
      SimpleBullet(string bulletName, float speed, float firePower, float damagePower) :
      Bullet(bulletName, speed, firePower, damagePower)
      {
      }
  
      unique_ptr&lt;Bullet&gt; clone() override
      {
          return make_unique&lt;SimpleBullet&gt;(*this);
      }
  };
  
  /** GoodBullet is the Concrete Prototype */
  class GoodBullet : public Bullet
  {
  
  public:
      GoodBullet(string bulletName, float speed, float firePower, float damagePower) 
      : Bullet(bulletName, speed, firePower, damagePower) 
      {
      }
  
      unique_ptr&lt;Bullet&gt; clone() override
      {
          return std::make_unique&lt;GoodBullet&gt;(*this);
      }
  };
  
  
  /** Opaque Bullet type, avoids exposing concrete implementations */
  enum BulletType
  {
      SIMPLE,
      GOOD
  };
  
  /** BulletFactory is the client */
  class BulletFactory
  {
  private:
      unordered_map&lt;BulletType, unique_ptr&lt;Bullet&gt;, hash&lt;int&gt; &gt; m_Bullets;
  
  public:
      BulletFactory()
      {
          m_Bullets[SIMPLE] = make_unique&lt;SimpleBullet&gt;("Simple Bullet", 50, 75, 75);
          m_Bullets[GOOD]   = make_unique&lt;GoodBullet&gt;("Good Bullet", 75, 100, 100);
      }
  
      unique_ptr&lt;Bullet&gt; createBullet(BulletType BulletType)
      {
          return m_Bullets[BulletType]-&gt;clone();
      }
  };
  
  int main()
  {
      BulletFactory bulletFactory;
  
      auto Bullet = bulletFactory.createBullet(SIMPLE);
      Bullet-&gt;fire(90);
  
      Bullet = bulletFactory.createBullet(GOOD);
      Bullet-&gt;fire(100);
  }
  </code></pre>