Bootstrap

C++ 类静态函数访问类非静态成员变量

C++静态成员函数访问非静态成员的几种方法_wolf鬼刀的博客-CSDN博客_静态成员函数调用非静态成员函数

最近做网络通信,其中需要做个架构包含回调函数,里面有个静态函数,网络数据传入这个静态函数,然后再让这个静态函数调用回调函数把数据传到另外一个类。 使用链接上的第三个方法,最后还是要在cpp里去申明一下静态类,不然还是会报错:

[Solved]-Undefined reference to static class member-C++ (appsloveworld.com)

具体如下:

1. 头文件声明class:

其中class内包含静态函数:

 2.  在头文件内声明静态class:

public:
	static MqttCommunicationInterface *ptrForStatic_;

3. 在class的cpp声明这个静态类,在构造函数里把当前类指针赋值到静态类的指针即可

MqttCommunicationInterface *MqttCommunicationInterface::ptrForStatic_;

MqttCommunicationInterface::MqttCommunicationInterface()
{
	ptrForStatic_ = this;
    。。。。
}
;