c++使用map保存成员函数地址
2021/8/29 1:36:09
本文主要是介绍c++使用map保存成员函数地址,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
note
- 本基于c++11介绍一种使用map保存成员函数地址
- 可避免使用 if 和 switch
- 配置灵活 方便,
- 代码维护效率高
结果:
范例开始
头文件包含
#include <iostream> #include <map> #include <algorithm>
必要类型前置声明
class pop_input_ui; /// 前置声明 typedef void (pop_input_ui::*psend_cmd)(); /// ---------------------------------------------------------------------------- /// @brief: 用于map保存成员函数地址 /// ---------------------------------------------------------------------------- struct st_send_cmd_config_ { psend_cmd psend_cmd_ = nullptr; void zero_() { psend_cmd_ = nullptr; } st_send_cmd_config_() { zero_(); } }; using send_cmd_config = st_send_cmd_config_; /// 用于保存成员函数 using map_send_cmd = std::map<int , send_cmd_config>;
类的完整定义
/// ---------------------------------------------------------------------------- /// @brief: 通用弹窗 /// ---------------------------------------------------------------------------- class pop_input_ui { public: /// ---------------------------------------------------------------------------- /// @brief: 发送类型枚举 /// ---------------------------------------------------------------------------- enum en_send_type { ksend_type_01 = 1 , ksend_type_02 = 2 , ksend_type_03 = 3 , }; /// ---------------------------------------------------------------------------- /// @brief: 构造函数 /// ---------------------------------------------------------------------------- pop_input_ui() { /// ---------------------------------------------------------------------------- /// @brief: 向map中添加item /// ---------------------------------------------------------------------------- auto map_insert = [&](int map_key, psend_cmd pfunc) { send_cmd_config config; config.psend_cmd_ = pfunc; std::pair<int, send_cmd_config> map_pair = {map_key, config}; map_send_cmd_.insert(map_pair); }; /// ---------------------------------------------------------------------------- /// 构建map, 将成员函数地址保存 map_insert(ksend_type_01, &pop_input_ui::send_cmd01_); map_insert(ksend_type_02, &pop_input_ui::send_cmd02_); map_insert(ksend_type_03, &pop_input_ui::send_cmd03_); } /// ---------------------------------------------------------------------------- /// @brief: 发送命令 /// ---------------------------------------------------------------------------- void send_cmd_(const en_send_type& type) { auto find_send_type = map_send_cmd_.find(type); if (find_send_type == map_send_cmd_.end()) return ; /// 找到了,则调用 if (nullptr == find_send_type->second.psend_cmd_) return; (this->*find_send_type->second.psend_cmd_)(); } private: /// ---------------------------------------------------------------------------- /// @brief: 发送cmd02 /// ---------------------------------------------------------------------------- void send_cmd01_() { cout << "\n send cmd 01\n"; } /// ---------------------------------------------------------------------------- /// @brief: 发送cmd02 /// ---------------------------------------------------------------------------- void send_cmd02_() { cout << "\n send cmd 02\n"; } /// ---------------------------------------------------------------------------- /// @brief: 发送cmd03 /// ---------------------------------------------------------------------------- void send_cmd03_() { cout << "\n send cmd 03\n"; } private: /// 用于保存成员函数地址 map_send_cmd map_send_cmd_; };
main 函数调用
int main() { /// 调用范例 pop_input_ui pop_ui; /// 调用发送命令01 pop_ui.send_cmd_(pop_input_ui::ksend_type_01); /// 发送命令02 pop_ui.send_cmd_(pop_input_ui::ksend_type_02); /// 发送命令03 pop_ui.send_cmd_(pop_input_ui::ksend_type_03); system("pause"); return 0; }
完整源码
#include <iostream> #include <map> #include <algorithm> using namespace std; class pop_input_ui; /// 前置声明 typedef void (pop_input_ui::*psend_cmd)(); /// ---------------------------------------------------------------------------- /// @brief: 用于map保存成员函数地址 /// ---------------------------------------------------------------------------- struct st_send_cmd_config_ { psend_cmd psend_cmd_ = nullptr; void zero_() { psend_cmd_ = nullptr; } st_send_cmd_config_() { zero_(); } }; using send_cmd_config = st_send_cmd_config_; /// 用于保存成员函数 using map_send_cmd = std::map<int , send_cmd_config>; /// ---------------------------------------------------------------------------- /// @brief: 通用弹窗 /// ---------------------------------------------------------------------------- class pop_input_ui { public: /// ---------------------------------------------------------------------------- /// @brief: 发送类型枚举 /// ---------------------------------------------------------------------------- enum en_send_type { ksend_type_01 = 1 , ksend_type_02 = 2 , ksend_type_03 = 3 , }; /// ---------------------------------------------------------------------------- /// @brief: 构造函数 /// ---------------------------------------------------------------------------- pop_input_ui() { /// ---------------------------------------------------------------------------- /// @brief: 向map中添加item /// ---------------------------------------------------------------------------- auto map_insert = [&](int map_key, psend_cmd pfunc) { send_cmd_config config; config.psend_cmd_ = pfunc; std::pair<int, send_cmd_config> map_pair = {map_key, config}; map_send_cmd_.insert(map_pair); }; /// ---------------------------------------------------------------------------- /// 构建map, 将成员函数地址保存 map_insert(ksend_type_01, &pop_input_ui::send_cmd01_); map_insert(ksend_type_02, &pop_input_ui::send_cmd02_); map_insert(ksend_type_03, &pop_input_ui::send_cmd03_); } /// ---------------------------------------------------------------------------- /// @brief: 发送命令 /// ---------------------------------------------------------------------------- void send_cmd_(const en_send_type& type) { auto find_send_type = map_send_cmd_.find(type); if (find_send_type == map_send_cmd_.end()) return ; /// 找到了,则调用 if (nullptr == find_send_type->second.psend_cmd_) return; (this->*find_send_type->second.psend_cmd_)(); } private: /// ---------------------------------------------------------------------------- /// @brief: 发送cmd02 /// ---------------------------------------------------------------------------- void send_cmd01_() { cout << "\n send cmd 01\n"; } /// ---------------------------------------------------------------------------- /// @brief: 发送cmd02 /// ---------------------------------------------------------------------------- void send_cmd02_() { cout << "\n send cmd 02\n"; } /// ---------------------------------------------------------------------------- /// @brief: 发送cmd03 /// ---------------------------------------------------------------------------- void send_cmd03_() { cout << "\n send cmd 03\n"; } private: /// 用于保存成员函数地址 map_send_cmd map_send_cmd_; }; int main() { /// 调用范例 pop_input_ui pop_ui; /// 调用发送命令01 pop_ui.send_cmd_(pop_input_ui::ksend_type_01); /// 发送命令02 pop_ui.send_cmd_(pop_input_ui::ksend_type_02); /// 发送命令03 pop_ui.send_cmd_(pop_input_ui::ksend_type_03); system("pause"); return 0; }
这篇关于c++使用map保存成员函数地址的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-06-29易优CMS安装常见问题汇总-icode9专业技术文章分享
- 2024-06-28易优新手必读安装教程-icode9专业技术文章分享
- 2024-06-28忘记eyoucms后台密码怎么办?-icode9专业技术文章分享
- 2024-06-26终极指南:Scrum中如何设置需求优先级
- 2024-06-26AI大模型企业应用实战(25)-为Langchain Agent添加记忆功能
- 2024-06-26小白家庭 nas 搭建方案-icode9专业技术文章分享
- 2024-06-23AI大模型企业应用实战(14)-langchain的Embedding
- 2024-06-23AI大模型企业应用实战(15)-langchain核心组件
- 2024-06-23AI大模型企业应用实战(16)-langchain核心组件
- 2024-06-23AI 大模型企业应用实战(06)-初识LangChain