title: C++: Calling member function via pointer
body: <p>I have this example code of using pointer to member function, which I want to change during runtime, but I cannot make it work. I've already tried <code>this-&gt;*_currentPtr(4,5)</code> <code>(*this)._currentPtr(4, 5)</code>. What is the proper way of calling pointer to method inside same class ?</p>
  
  <p>The error : <strong><em>expression must have (pointer-to-) function type</em></strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  
  class A {
  
  public:
  
      void setPtr(int v);
      void useFoo();
  
  private:
      typedef int (A::*fooPtr)(int a, int b);
  
      fooPtr _currentPtr;
  
      int foo1(int a, int b);
      int foo2(int a, int b);
  };
  
  void A::setPtr(int v){
      if(v == 1){
          _currentPtr = foo1;
      } else {
          _currentPtr = foo2;
      }
  }
  
  void A::useFoo(){
  
      //std::cout &lt;&lt; this-&gt;*_currentPtr(4,5); // ERROR
  }
  
  int A::foo1(int a, int b){
      return a - b;
  }
  
  int A::foo2(int a, int b){
      return a + b;
  }
  
  int main(){
  
      A obj;
  
      obj.setPtr(1);
      obj.useFoo();
  
      return 0;
  }
  </code></pre>