title: Factory method implementation - C++
body: <p>I have the following code for "factory" design pattern implementation.</p>
  
  <pre><code>class Pen{
  public:
       virtual void Draw() = 0;
  };
  
  class RedPen : public Pen{
  public:
       virtual void Draw(){
           cout &lt;&lt; "Drawing with red pen" &lt;&lt; endl;
       }
  };
  
  class BluePen : public Pen{
  public:
       virtual void Draw(){
           cout &lt;&lt; "Drawing with blue pen" &lt;&lt; endl;
       }
  };
  
  auto_ptr&lt;Pen&gt; createPen(const std::string color){
       if(color == "red")
           return auto_ptr&lt;Pen&gt;(new RedPen);
       else if(color == "blue")
           return auto_ptr&lt;Pen&gt;(new BluePen);
  }
  </code></pre>
  
  <p>But I heard that it can be done in a better way using "C++ templates". Can anyone help how it is done and how template approach is better than this?</p>
  
  <p>Any thoughts</p>