如何从另一个成员函数调用成员函数指针?

时间:2021-06-02 10:25:57

标签: c++ pointers member-function-pointers

我有一个类,它有一个存储成员函数指针的数据成员。指针在不同的时间指向不同的成员函数。我想从另一个成员函数调用这个函数指针。我该怎么做?

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(){ ... }

}

   

1 个答案:

答案 0 :(得分:2)

您需要指定在哪个对象上调用函数:

(this->*fnPointer)();
相关问题