title: Overriding a pure virtual function from templated classes
body: <p>I have searched stack overflow but haven't found something that exactly answers my question.  I have an interface class which contains only pure virtual functions which I would like to be implemented by classes which derive from this class. </p>
  
  <p>I have an interface that I will call <strong>BaseInterface</strong> which defines functions that I would like to be overridden in all classes which derive from this interface.  In this example, say there is only one pure virtual function called <em>toImplement</em>.  I create a class called <strong>Base</strong> which inherits from <strong>BaseInterface</strong> and adds some functionality to the inherited pure virtual functions.  Base is still an abstract class since it does not implement the functions in <strong>BaseInterface</strong>.</p>
  
  <p>I have several classes which derive from <strong>Base</strong> which all benefit from the common functionality of <strong>Base</strong> but specify what happens when <em>toImplement</em> is run on their instances.  These classes should all be concrete and satisfy all the requirements set by <strong>BaseInterface</strong>. Below I define one of these classes called <strong>Derived</strong>.</p>
  
  <p>All of this works fine when BaseInterface and Base are not templated.  The code compiles and runs fine without defining (1.) or implementing (2.) <em>toImplement</em> in <strong>Base</strong>.  </p>
  
  <p>However I would like <em>toImplement</em> to work with different types.  From what I understand it is fine to have pure virtual functions in a templated class.  I template <strong>BaseInterface</strong> and <strong>Base</strong> on some type T.  When I don't define <em>toImplement</em> in <strong>Base</strong> (1.), I cannot compile since Base doesn't know which <em>toImplement</em> to use in <em>tryUsingImplemented</em>.  If I now add the definition to <strong>Base</strong>, the code pre-compiles but the linker cannot find the implementation of Base::toImplement.  Finally, if I both define and implement <em>toImplement</em> in <strong>Base</strong> (1. and 2.), the code compiles.</p>
  
  <p>I don't like this because I have a dummy implementation of <em>toImplement</em> in Base and I never want this implementation to be run.  Additionally, because <strong>Base</strong> implements <em>toImplement</em>, <strong>Derived</strong> is no longer required to implement it.  This makes <strong>BaseInterface</strong> useless in my eyes.</p>
  
  <p>Can sombody enlighten me on how to enforce the implementation of <em>toImplement</em> in <strong>Derived</strong>, without having to implement it in <strong>Base</strong> first if that is at all possible?</p>
  
  <pre><code>template &lt;typename T&gt;
  class BaseInterface {
     virtual void toImplement(T &amp; t) = 0;
  };
  
  
  template &lt;typename T&gt;
  class Base : public BaseInterface&lt;T&gt; {
  bool m_useImplemented;
  
  public:
     explicit Base(bool useImplemented) : m_usedImplemented(useImplemented) {}
  
     void tryUsingImplemented(T &amp; t) {
        if (m_useImplemented)
           toImplement(t);
     }  
  
  protected:
     // 1: Defining toImplement pure virtual function in Base
     virtual void toImplement(T &amp; t);
  };
  
  // 2. Implementing a version of toImplement in Base which does nothing
  template &lt;typename T&gt;
  inline void Base&lt;T&gt;::toImplement(T &amp; t) {
     // do nothing
  }
  
  class Derived : public Base&lt;int&gt; {
  public:
     explicit Derived(bool useImplemented) : Base&lt;int&gt;(useImplemented) {}
  
  protected:
     // 3. implementing toImplement in Derived 
     void toImplement(T &amp; t) {
        std::cout &lt;&lt; "Doing stuff for Derived" &lt;&lt; std::endl;
     }  
  
  };
  </code></pre>