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;
}

最新文章

  1. Linux下怎么运行java程序
  2. NGUI学习笔记(二):基础笔记
  3. Android动态加载代码技术
  4. 新注册第一帖----------------------乱码新手自学.net 之Linq 入门篇
  5. Vim 命令图解-Gvim使用笔记-2017-5-9
  6. 简易版jQuery——mQuery
  7. 用gcc编译c语言程序以及其编译过程
  8. esxi 改变虚拟机磁盘格式为精简存储
  9. iis7.5做反向代理配置方法实例图文教程
  10. 模块math和cmath
  11. 生成证书申请csr文件
  12. springboot - mybatis - 下划线与驼峰自动转换 mapUnderscoreToCamelCase
  13. js点击添加
  14. java基础----&gt;多线程之Runnable(一)
  15. Odoo中如何复制有唯一性约束的记录?
  16. ORACLE常用性能监控SQL(二)
  17. 10 Things ASP.NET Developers Should Know About Web.config Inheritance and Overrides(转)
  18. C语言中的输入方式
  19. linux 入门测验
  20. Hbase和RDBMS(关系数据库管理系统)区别

热门文章

  1. Codeforces Gym 101175F - Machine Works(CDQ 分治维护斜率优化)
  2. 【豆科基因组】利马豆/洋扁豆Lima bean(Phaseolus lunatus L.)基因组2021NC
  3. three.js很好玩
  4. 02-爬取http://www.allitebooks.org/网站,获取图片url,书名,简介,作者
  5. scp命令的简单使用
  6. ReactiveCocoa操作方法-秩序
  7. android:为TextView添加样式、跑马灯、TextSwitcher和ImageSwitcher实现平滑过渡
  8. 【Linux】【Services】【MessageQueue】搭建高可用rabbitMQ
  9. vue实现input输入框的模糊查询
  10. 通过 Ajax 发送 PUT、DELETE 请求的两种实现方式