我想通过member-function-pointers调用成员函数.调用函数也是成员.
class A;
typedef int (A::*memFun)();
class A
{
int P(){return 1;}
int Q(){return 2;}
int R(){return 3;}
int Z(memFun f1, memFun f2)
{
return f1() + f2(); //HERE
}
public:
int run();
};
int A::run()
{
return Z(P, Q);
}
int main()
{
A a;
cout << a.run() << endl;
}
Run Code Online (Sandbox Code Playgroud)
我没有正确地做到这一点,并且我得到错误 -
main.cpp:15:19: error: must use '.*' or '->*' to call pointer-to-member function in 'f1 (...)', e.g. '(... ->* f1) (...)'
return f1() + f2(); //HERE
Run Code Online (Sandbox Code Playgroud)
请说明正确的方法.
编辑 - 还有另一个错误,通过以下方式解决
return Z(&A::P, &A::Q);
Run Code Online (Sandbox Code Playgroud)
Arm*_*yan 19
(this->*f1)() + (this->*f2)();
Run Code Online (Sandbox Code Playgroud)
无论您是从类中调用它,都必须明确指定要调用的对象(在本例中this).另请注意所需的括号.以下是错误的:
this->*f1() + this->*f2()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11404 次 |
| 最近记录: |