Search code examples
c++pointerspointer-to-member

How can i call a member function pointer from another member function?


I have a class which has a data memeber which store a member function pointer. The pointer points to different member functions in different time. I would like to call this funcion pointer from another member function. How can i do this?

class A {

  void (A::*fnPointer)() = nullptr;

  void callerMemberFunction(){
    fnPointer = &A::someMemberFunction;  // Store the function
    (*fnPointer)(); // I would like to call the function. How can i do?
  }

  void someMemberFunction(){ ... }

}

   

Solution

  • You need to specify which object to call the function on:

    (this->*fnPointer)();