C++ 静态成员变量和静态成员函数

#include<iostream>
using namespace std;

class test
{
public:
	//静态成员函数
	//静态成员函数只能调用静态成员变量 不能调用普通成员变量
	//普通变量为每个对象本身所拥有,而静态成员变量为所有对象共同拥有
	//如果静态成员函数调用普通变量的话,静态函数不知道自己调用的是哪一个对象的变量
	static void printfC1()
	{
		cout <<"M_c="<<m_c << endl;
	}
	//普通成员函数
	void printfC()
	{
		cout <<"m_c="<<m_c << endl;
	}
	void  AddC()
	{
		m_c = m_c + 1;
	}
private:
	int m_a;
	int m_b;
	static int m_c;
};
int test::m_c = 10;

void main()
{
	test t1, t2, t3;
	t1.printfC();
	t2.AddC();
	t3.printfC();
	//静态成员函数调用方法
	test::printfC1();
	t1.printfC1();
	system("pause");
}

猜你喜欢

转载自blog.csdn.net/error0_dameng/article/details/82054200