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
静态变量的可以定义在类内,可以定义在函数内,是类共有的。只初始化一次。生命周期是整个程序。
静态变量的初始化在类外。