【发布时间】:2013-08-11 08:06:05
【问题描述】:
我编写了一个小程序,试图将指向类成员函数的指针传递给另一个函数。你能帮我吗,我哪里出错了..?
#include<iostream>
using namespace std;
class test{
public:
typedef void (*callback_func_ptr)();
callback_func_ptr cb_func;
void get_pc();
void set_cb_ptr(void * ptr);
void call_cb_func();
};
void test::get_pc(){
cout << "PC" << endl;
}
void test::set_cb_ptr( void *ptr){
cb_func = (test::callback_func_ptr)ptr;
}
void test::call_cb_func(){
cb_func();
}
int main(){
test t1;
t1.set_cb_ptr((void *)(&t1.get_pc));
return 0;
}
当我尝试编译时出现以下错误。
error C2276: '&' : illegal operation on bound member function expression
【问题讨论】:
-
我总觉得newty.de/fpt/index.html很有用。
-
成员函数不是函数。你需要的类型是
void (test::*)(void *)... -
Kerrek SB 是对的。但是,如果您打算为不同的类和实例调用相同的成员,您应该考虑继承和虚拟...
标签: c++ function-pointers