C++でオブザーバパターン(ソート)を実装しようとしていますが、関数ポインタを使用してキャストしようとするとエラーが発生します。 typedefの関数ポインタへのクラスBから関数ポインタ:typedef関数ポインタを使用してコールバックを登録する方法
#include <map>
typedef int (*OutputEvent)(const char*, const char*, int);
class A
{
private:
int nextListenerId;
std::map<int, OutputEvent> listenerMap;
public:
A(){ nextListenerId = 0;}
~A(){}
inline int RegisterListener(OutputEvent callback)
{
nextListenerId++;
listenerMap[nextListenerId] = callback;
return nextListenerId;
}
};
class B
{
private:
int listenerId;
public:
B(const A& a)
{
OutputEvent e = &B::CallMeBack;
listenerId = a.RegisterListener(e);
}
~B(){}
int CallMeBack(const char* x, const char* y, int z)
{
return 0;
}
};
私はこの例を作成したと私はpasted it into codepad.orgをしましたが、私はそれが(それがcodepad.org中にも、Visual Studioでコンパイルできないコンパイルに失敗した場合2010):
私はfunctioを変換できない理由がわかりませんn個のポインタ。誰も私を助けてくれますか?
これは前に何度も尋ねられています:http://stackoverflow.com/questions/4210710/cast-pointer-to-member-function-to-normal-pointer and more。 http://www.parashift.com/c++-faq-lite/pointers-to-members.htmlも参照してください。 –
http://stackoverflow.com/questions/1151582/pthread-function-from-a-classも参照してください。 –