今天遇到的一个函数指针的问题

    技术2022-05-11  49

    #include <map>#include <string>using namespace std;

    //事件响应回调函数typedef bool (__cdecl * EventResponseFunc)(const char * eventname);

    //写着玩的。:)

    //这是一个事件系统的回调功能的实现;可将函数名称作为参数进行调用,其中适用的就是函数指针的机制。

    //也可以进行分类,将事件分类,然后使用详细事件,在每个功能函数中进行分析,再进行不同的处理;

    //这里只是一个模型,怎么应用就靠大家去发挥了。祝各位好运喽!

    class EventSystem{public: static void DeclareEventFunc(const char * funcname, EventResponseFunc func) {  EventSystem::eventfunc_map[funcname] = func; }  static bool DoEventFunc(const char * funcname, const char * eventname) {  EventResponseFunc func = EventSystem::eventfunc_map[funcname];  return (func)(eventname); }protected: static map <string, EventResponseFunc> eventfunc_map;};

    map <string, EventResponseFunc> EventSystem::eventfunc_map;

    #define DECL_EVENT_FUNC(p) EventSystem::DeclareEventFunc(#p, p);

    bool Event_Test1(const char * eventname){ ::printf("Event_Test1"); return true;}

    bool Event_Test2(const char * eventname){ ::printf("Event_Test2"); return true;}

    bool Event_Test3(const char * eventname){ ::printf("Event_Test3"); return true;}

    int _tmain(int argc, _TCHAR* argv[]){ DECL_EVENT_FUNC(Event_Test1) return 0;} 


    最新回复(0)