我有一个类,它有一个存储成员函数指针的数据成员。指针在不同的时间指向不同的成员函数。我想从另一个成员函数调用这个函数指针。我该怎么做?
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(){ ... }
}
答案 0 :(得分:2)
您需要指定在哪个对象上调用函数:
(this->*fnPointer)();