静态变量的特点

此生再无相见时 提交于 2019-12-27 18:54:18

静态变量的特点

code

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

#include "stdafx.h"
#include <iostream>
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

在这里插入图片描述
静态变量的可以定义在类内,可以定义在函数内,是类共有的。只初始化一次。生命周期是整个程序。

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!