采用C++模板,预先在结构体中定义C++类成员函数指针

1. 模板结构体定义

//模板:主题与C++类函数映射关系
template <typename T>
struct TMatchTopicFunc {
	string	strTopic;
	void	(T::*ProcHook)(TMqttMsg &, TTopic &);
};

2. 定义个C++类

class CRealtime
{
    //...
public:
	//描述:实时数据交互接口
	void Add(TMqttMsg &mqttMsg, TTopic &topic){};
	void Query(TMqttMsg &mqttMsg, TTopic &topic){};

    //...
};

3. 定义一个结构体struct TMatchTopicFunc变量

struct TMatchTopicFunc<CRhRealtime> matchTopicFunc[3] = {
	//实时数据操作
	{ "notify/event/database/",					&CRhRealtime::Add },
	{ "get/request/database/realtime",			&CRhRealtime::Query },
};

总结:由于要使用C++类成员函数指针,必须实现定义类,为避免这种情况,可以在结构体中引入模板,模板类型即为对应的C++类

猜你喜欢

转载自blog.csdn.net/chenliang0224/article/details/113472759