title: C++ Interface vs Template
body: <p>I have 2 solutions for the same problem - to make some kind of callbacks from one "controller" to the used object and I don't know what to chose.</p>
  
  <p>Solution 1: Use interfaces</p>
  
  <pre><code>struct AInterface
  {
      virtual void f() = 0;
  };
  
  struct A : public AInterface
  {
      void f(){std::cout&lt;&lt;"A::f()"&lt;&lt;std::endl;}
  };
  
  struct UseAInterface
  {
      UseAInterface(AInterface* a) : _a(a){}
      void f(){_a-&gt;f();}
  
      AInterface* _a;
  };
  </code></pre>
  
  <p>Solution 2: Use templates</p>
  
  <pre><code>struct A
  {
      void f(){std::cout&lt;&lt;"A::f()"&lt;&lt;std::endl;}
  };
  
  template&lt;class T&gt;
  struct UseA
  {
      UseA(T* a) : _a(a){}
      void f(){_a-&gt;f();}
  
      T* _a;
  };
  </code></pre>
  
  <p>This is just a simple sample to illustrate my problem. In real world the interface will have several functions and one class may(and will!) implement multiple interfaces.</p>
  
  <p>The code will not be used as a library for external projects and I don't have to hide the template implementation - I say this because first case will be better if I need to hide "controller" implementation.</p>
  
  <p>Can you please tell me the advantages/disadvantages for each case and what is better to use?</p>