title: Policy based design and best practices - C++
body: <pre><code>struct InkPen
  {
    void Write()
    {
      this-&gt;WriteImplementation();
    }
  
    void WriteImplementation()
    {
      std::cout &lt;&lt; "Writing using a inkpen" &lt;&lt; std::endl;
    }
  
  };
  
  struct BoldPen
  {
    void Write()
    {
      std::cout &lt;&lt; "Writing using a boldpen" &lt;&lt; std::endl;
    }
  };
  
  template&lt;class PenType&gt;
  class Writer : public PenType
  {
  public:
    void StartWriting()
    {
      PenType::Write();
    }
  };
  
  int main()
  {
    Writer&lt;InkPen&gt; writer;
    writer.StartWriting();
    Writer&lt;BoldPen&gt; writer1;
    writer1.StartWriting();
    return 0;
  }
  </code></pre>
  
  <p>I wrote the above code as part of learning <em>policy based designs</em>. I have few questions on the above code</p>
  
  <p>1 - Does this implementation look correct? I mean: does it really look like a policy based design?</p>
  
  <p>2 - I can now hook any kind of pens to writer. But what will I do when I got a pen with no default constructor (only parameterized constructors)? How will I handle this situation?</p>
  
  <pre><code>template&lt;class PenType&gt;
  class Writer : public PenType
  {
  public:
    void StartWriting()
    {
      PenType::Write();
    }
  };
  </code></pre>
  
  <p>3 - When the above code is used like</p>
  
  <pre><code>Writer&lt;InkPen&gt; writer;
  </code></pre>
  
  <p>I guess compiler will replace <em>PenType</em> with <em>InkPen</em>. If yes, why I am not able to call just <em>Write()</em> from <em>StartWriting()</em> instead of prefixing base class name (<em>PenType::Write()</em>)? </p>
  
  <p>4 - I think policy based design forces you to derive from classes which is semantically invalid. In the above code, a writer is derived from a pen only because writer uses a pen. But saying writer is a pen is semantically invalid. Is there any other better way to address this or I am missing something here?</p>
  
  <p>Any thoughts?</p>