今天遇到的一个函数指针的问题
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;
}
#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;
}
相关文章推荐
- 函数返回值为指针的一个陷阱(使用inet_ntoa函数时遇到的问题)
- 当缩放遇到Texture2D drawAtPoint 函数的一个问题.
- 今天遇到一个奇怪的问题!!无奈搞了半天也没解决,难道非要重装
- 今天遇到了一个问题,将应用程序从服务器读取到的电话号码存储到通讯录中,必须在真机上跑,有点小激动。
- 今天遇到的一个关于HttpSessionListener 的一个问题
- 记录下今天调试USB读取FIFO函数遇到的问题
- 今天遇到了一个问题,怎么判断你输入的数是整数 ----2012.6.28
- 今天在写一个关于背四级单词的APP,遇到一个问题
- 网易面试题之小易是一个数论爱好者,并且对于一个数的奇数约数十分感兴趣。一天小易遇到这样一个问题: 定义函数f(x)为x最大的奇数约数,x为正整数。 例如:f(44) = 11. 现在给出一个N,需要求
- 今天在看一些面试题的时候遇到的一个关于strcmp()返回值的细节问题
- 今天遇到的一个奇葩的NoClassFound的问题
- 初学SSDT遇到的一个指针问题
- 今天实现二叉树的时候遇到了一个很有意思的问题
- 今天工作需要调用一个c++程序,以给那个程序发送请求的方式,遇到了跨域问题
- VMware启动时今天遇到一个很恶心的问题
- \t\t今天遇到了一个很奇怪的问题
- 今天遇到的一个浏览器会记录密码缓存的问题
- 今天遇到一个问题,获取properties配置文件中的中文时会出现乱码的情况
- 今天遇到一个神奇的问题--万能的alert出问题了,javascript怎么办?
- 用委托改写一个经典函数指针问题