map管理成员函数指针
2016-07-18 18:03
232 查看
转自:http://bbs.csdn.net/topics/290032347
概括:将成员函数指针名和同名的string类型的变量名进行绑定,插入到map中。
在使用时就可以通过找到string类型的变量名来使用同名的函数
概括:将成员函数指针名和同名的string类型的变量名进行绑定,插入到map中。
在使用时就可以通过找到string类型的变量名来使用同名的函数
#include "iostream" #include "string" #include "map" using namespace std; class test { public : void fun1() { cout<<"call test::fun1"<<endl; } void fun2() { cout<<"call test::fun2"<<endl; } void fun3() { cout<<"call test::fun3"<<endl; } test() { m_mapFun["test::fun1"] = &test::fun1; m_mapFun["test::fun2"] = &test::fun2; m_mapFun["test::fun3"] = &test::fun3; } void call(string strfun) { if (m_mapFun.find(strfun) == m_mapFun.end()) cout<<"no function : "<<strfun<<endl; else (this->*m_mapFun[strfun])(); } protected : typedef void (test::*mfun)(); map<string, mfun> m_mapFun; }; int main() { test t; t.call("test::fun1"); t.call("test::fun2"); t.call("test::fun3"); t.call("test::fun4"); return 0; } ----------------- 结果: call test::fun1 call test::fun2 call test::fun3 no function : test::fun4
相关文章推荐
- angular的脏检查
- 小记——内存映射
- spark操作mysql数据库
- “区块链”将重新定义人类生活,你会看懂么?
- netdevgirl.eclipse创建maven javaweb项目
- Postfix邮件服务器搭建及配置
- python命令行下tab键补全命令
- javascript面试题:如何把一句英文每个单词首字母大写?
- Debain安装字体,修改默认编码,命令行
- hadoop伪分布式2.4.1安装
- ""和null的区别
- leetcode 262. Trips and Users
- BottomSheetDialog
- nginx生产环境常用功能include 、虚拟主机别名、rewrite、nginx status详细解析、fastcgi、访问控制
- RedHat Linux 手动设置IP及传送文件到远程主机
- git 操作
- eclipse里面已经提交的svn提交
- .net {"已添加项。字典中的关键字:“**”所添加的关键字:“**”"}
- Android Studio的使用
- 判断距离自己最近的点