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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM