函数指针:混了。。。
指针函数:混了。。。
//事件类--应该是单例
#pragma once #include <iostream> using namespace std; #include <string.h> #include <map> class MyEvent { public: MyEvent(){}; ~MyEvent(){}; public: //键值对存消息 map<string, void(*)(void)> mapTest2; public: //监听函数 void AddListener(string _eventName, void(*func)(void))//函数指针作为函数参数 { for (map<string, void(*)(void)>::iterator itr = mapTest2.begin(); itr != mapTest2.end(); itr++) { if (itr->first == _eventName) { return; } } mapTest2[_eventName] = (*func); } void Dispach(string eventName) { //遍历容器,找到对应的键如果有就执行 for (map<string, void(*)(void)>::iterator itr = mapTest2.begin(); itr != mapTest2.end(); itr++) { if (itr->first == eventName) //如果键等于传经来的键 { (mapTest2[eventName])(); return; } } cout << "没有这个键" << endl; } };Main函数:
#include "MyEvent.h" void Output(void) { cout << "系统测试成功!!!!!!" << endl; }
void outPut2(void(*fun)(void)) { fun(); }void main(){MyEvent test;test.AddListener("output", Output);test.Dispach("output");}