クラスのメンバ関数へのポインタを別の関数に渡そうとする小さなプログラムを作成しました。あなたは私を助けてくれるでしょうか、どこに間違っているのですか?クラスメンバ関数へのポインタをパラメータとして渡す
#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
私はいつもhttp://www.newty.de/fpt/index.htmlが非常に便利だと分かります。 – arne
メンバ関数は関数ではありません。必要な型は 'void(test :: *)(void *)'です... –
Kerrek SBが正しいです。しかし、異なるクラスやインスタンスに対して同じメンバを呼び出すつもりなら、継承と仮想を考えるべきです... –