title: Strategy Pattern in C++ with template
body: <p>This is a sample implementation of Strategy Pattern in C++:</p>
  
  <p>ConcreteStrategy.h</p>
  
  <pre><code>class ConcreteStrategy {
  public:
      ConcreteStrategy();
      ~ConcreteStrategy();
      const OtherObject* doSomething(const OtherObject &amp;obj);
  };
  </code></pre>
  
  <p>ConcreteStrategy.cpp</p>
  
  <pre><code>#include "ConcreteStrategy.h"
  
  ConcreteStrategy::ConcreteStrategy() { // etc. }
  ConcreteStrategy::~ConcreteStrategy() { // etc. }
  const OtherObject* ConcreteStrategy::doSomething(const OtherObject &amp;obj) { // etc. }
  </code></pre>
  
  <p>MyContext.h</p>
  
  <pre><code>template &lt;class Strategy&gt; class MyContext {
      public:
          MyContext();
          ~MyContext();
          const OtherObject* doAlgorithm(const OtherObject &amp;obj);
      private:
          Strategy* _the_strategy;
  };
  </code></pre>
  
  <p>MyContext.cpp</p>
  
  <pre><code>#include "MyContext.h"
  
  template &lt;typename Strategy&gt;
  MyContext&lt;Strategy&gt;::MyContext() {
      _the_strategy = new Strategy;
  }
  
  template &lt;typename Strategy&gt;
  MyContext&lt;Strategy&gt;::~MyContext() {
      delete _the_strategy;
  }
  
  template &lt;typename Strategy&gt;
  const OtherObject* MyContext&lt;Strategy&gt;::doAlgorithm(const OtherObject &amp;obj) {
      obj = _the_strategy(obj);
      // do other.
      return obj;
  }
  </code></pre>
  
  <p>main.cpp</p>
  
  <pre><code>#include "MyContext.h"
  #include "ConcreteStrategy.h"
  #include "OtherPrivateLib.h"
  
  int main(int argc,char **argv) {
      OtherObject* obj = new OtherObject;
      MyContext&lt;ConcreteStrategy&gt;* aContext = new MyContext&lt;ConcreteStrategy&gt;;
      obj = aContext.doAlgorithm(obj);
  
      // etc.
  
     delete aContext;
     delete obj;
  
     return 0;
  }
  </code></pre>
  
  <p>Is this implementation right? It's my first approach with template in C++ and I've some doubt, specially about construction and destruction of template object (Strategy) inside the context (MyContext).</p>
  
  <p>UPDATE:
  I've this error at compile-time:</p>
  
  <pre><code>undefined reference to `MyContext&lt;Strategy&gt;::MyContext()'
  </code></pre>