dee*_*eeJ 2 c++ member-function-pointers function-pointers function
我有一节课:
class A {
void test_func_0(int);
void run();
typedef void(A::*test_func_t)(int);
struct test_case_t{
test_func_t test_func;
} test_case[100];
};
Run Code Online (Sandbox Code Playgroud)
现在我想在run()中调用test_func():
void A::run()
{
test_case[0].test_func = &test_func_0;
test_case[0].*(test_func)(1);
}
Run Code Online (Sandbox Code Playgroud)
无论我尝试什么组合,我的代码的最后一行都不起作用(编译错误).
用这个:
void A::run()
{
test_case[0].test_func = &A::test_func_0;
(this->*(test_case[0].test_func))(1);
}
Run Code Online (Sandbox Code Playgroud)
请注意,您有2个错误.第一个是你如何形成成员函数指针.请注意,唯一的方法是使用&ClassName::FuncName,无论您是否在类范围内.&也是强制性的.
第二个是当您通过成员函数指针调用成员时,必须显式指定要调用成员函数的对象(在您的情况下为A类型).在这种情况下,您必须指定this(因为这是我们使用的指针->*而不是.*)
HTH
| 归档时间: |
|
| 查看次数: |
427 次 |
| 最近记录: |