title: Virtual Methods or Function Pointers
body: <p>When implementing polymorphic behavior in C++ one can either use a pure virtual method or one can use function pointers (or functors). For example an asynchronous callback can be implemented by:</p>
  <h1>Approach 1</h1>
  <pre><code>class Callback
  {
  public:
      Callback();
      ~Callback();
      void go();
  protected:
      virtual void doGo() = 0;  
  };
  
  //Constructor and Destructor
  
  void Callback::go()
  {
     doGo();
  }
  </code></pre>
  <p>So to use the callback here, you would need to override the doGo() method to call whatever function you want</p>
  <h1>Approach 2</h1>
  <pre><code>typedef void (CallbackFunction*)(void*)
  
  class Callback
  {
  public:
      Callback(CallbackFunction* func, void* param);
      ~Callback();
      void go();
  private:
     CallbackFunction* iFunc;
     void* iParam;
  };
  
  Callback::Callback(CallbackFunction* func, void* param) :
      iFunc(func),
      iParam(param)
  {}
  
  //Destructor
  
  void go()
  {
      (*iFunc)(iParam);
  }
  </code></pre>
  <p>To use the callback method here you will need to create a function pointer to be called by the Callback object.</p>
  <h1>Approach 3</h1>
  <p>[This was added to the question by me (Andreas); it wasn't written by the original poster]</p>
  <pre><code>template &lt;typename T&gt;
  class Callback
  {
  public:
      Callback() {}
      ~Callback() {}
      void go() {
          T t; t();
      }
  };
  
  class CallbackTest
  {
  public:
      void operator()() { cout &lt;&lt; &quot;Test&quot;; }
  };
  
  int main()
  {
      Callback&lt;CallbackTest&gt; test;
  
      test.go();
  }
  </code></pre>
  <p><strong>What are the advantages and disadvantages of each implementation?</strong></p>