Bootstrap

c++ 静态成员函数调用非静态成员函数/变量

test.h

class A
{
private:
	static A *s_gA;
	static A *s_gB;
	int m_num;
public:
	A();
	~A();
	static void FuncA();
	void FuncB();
	static A* GetInstance();
	
};

test.cpp

#include "test.h"
A* A::s_gA = NULL;
A::A()
{
	s_gA = this;
	m_num = 1;
}
void A::FuncA()
{
	s_gA->m_num += 1;
	cout << "FuncA m_num = "<< s_gA->m_num << endl;
	s_gA->FuncB();
	return;
}

void A::FuncB()
{
	cout << "FuncB" <<endl;
	return;
}

A* A::GetInstance()
{
	if (!s_gb)
	{
		s_gb = new A;
	}
	return s_gb;
}

A::~A()
{

}

使用

#include <iostream>
#include "test.h"

int main()
{
	A myTest;
	myTest.FuncA();
    std::cout << "Hello World!\n";
	getchar();
}

https://blog.csdn.net/qq_43491149/article/details/121180390

参考

;