c++使用map保存成员函数地址

note

  • 本基于c++11介绍一种使用map保存成员函数地址
  • 可避免使用 if 和 switch
  • 配置灵活 方便,
  • 代码维护效率高

结果:

c++使用map保存成员函数地址

范例开始

头文件包含

#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 01n";
	}

	/// ----------------------------------------------------------------------------
	/// @brief: 发送cmd02
	/// ----------------------------------------------------------------------------
	void send_cmd02_()
	{
		cout << "n send cmd 02n";
	}

	/// ----------------------------------------------------------------------------
	/// @brief: 发送cmd03
	/// ----------------------------------------------------------------------------
	void send_cmd03_()
	{
		cout << "n send cmd 03n";
	}


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 01n";
	}

	/// ----------------------------------------------------------------------------
	/// @brief: 发送cmd02
	/// ----------------------------------------------------------------------------
	void send_cmd02_()
	{
		cout << "n send cmd 02n";
	}

	/// ----------------------------------------------------------------------------
	/// @brief: 发送cmd03
	/// ----------------------------------------------------------------------------
	void send_cmd03_()
	{
		cout << "n send cmd 03n";
	}


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

原文链接: https://www.cnblogs.com/pandamohist/p/15201705.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    c++使用map保存成员函数地址

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/213328

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月13日 上午1:42
下一篇 2023年2月13日 上午1:42

相关推荐