だから私はこのイベント管理クラスに取り組んでいます。署名のメンバー関数へのポインターのリストを保存していますvoid (Event*)
。ここで、イベントは現時点でランダムなデータを保存する単なる構造体です。
typedef boost::function<void(Event*)> Callback;
typedef vector<Callback> CallbackList;
class EventManager
{
public:
template<typename T>
void RegisterEventHandler(const std::string& type, void (T::*handler)(Event*), T* obj)
{
mCallbackList[type].push_back(boost::bind(handler, obj, _1));
}
void DispatchEvent(const std::string& type, Event* evt)
{
for(CallbackList::iterator it = mCallbackList[type].begin(); it != mCallbackList[type].end(); ++it)
{
Callback callback = (*it);
callback(evt);
}
}
private:
hash_map<std::string, CallbackList> mCallbackList;
};
異なるバージョンの Event を派生させ、それらのメンバー関数へのポインターをこのクラスに渡すことができるかどうか疑問に思っています。現在、私はこれを試しています。
class MouseEvent : public Event
{
public:
int testMouseData1;
int testMouseData2;
int testMouseData3;
};
class HelloWorld
{
public:
void Display(MouseEvent* evt)
{
cout << "Hello, world!" << endl;
}
};
int main(void)
{
MouseEvent* evt = new MouseEvent();
HelloWorld* world = new HelloWorld();
eventManager->RegisterEventHandler("testType", &HelloWorld::Display, world);
return 0;
}
これにより、XCode で次のエラーが発生します。
EventManager::RegisterEventHandler(const char [9], void (HelloWorld::*)(MouseEvent*), HelloWorld*&)
エラー: ' ' の呼び出しに一致する関数がありません
関数シグネチャで派生クラスを期待しているポインターを安全に渡す方法を知っていますか? ありがとう。