您的位置:首页 > 其它

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

2007-02-27 14:06 639 查看
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐