给定一个示例类:
class Fred
{
public:
Fred()
{
func = &Fred::fa;
}
void run()
{
int foo, bar;
*func(foo,bar);
}
double fa(int x, int y);
double fb(int x, int y);
private:
double (Fred::*func)(int x, int y);
};
我在通过指针“*func(foo,bar)”调用成员函数的那一行遇到编译器错误,说:“term does not evaluate to a function taking 2 arguments”。我做错了什么?
最佳答案
你需要的语法如下:
((object).*(ptrToMember))
所以你的电话是:
((*this).*(func))(foo, bar);
我相信另一种语法是:
(this->*func)(foo, bar);
关于c++ - 在类中使用成员函数指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2898316/