静态变量的特点

静态变量的特点

  • code
  • Result

code

// c1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include 
using namespace std;

class A
{
public:
	void Fun()
	{
		static int i_s(0);
		i_s++;
		cout << i_s << endl;
	}
};
int _tmain(int argc, _TCHAR* argv[])
{
	A a;
	a.Fun();

	A b;
	b.Fun();

	A c;
	c.Fun();

	system("pause");

	return 0;
}

Result

静态变量的特点_第1张图片
静态变量的可以定义在类内,可以定义在函数内,是类共有的。只初始化一次。生命周期是整个程序。
静态变量的初始化在类外。

你可能感兴趣的:(c++,c++)