欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C++语言之静态变量的运用

程序员文章站 2022-03-09 16:08:56
...
#include <iostream>

using namespace  std ; 

class  Banana
{
	public:

	static int id ; 

	Banana(void)
	{
		id++ ; 
		cout << "this is a test: " << id << endl ; 
	}
};

//类中的静态变量,必须在全局定义
int Banana::id = 0 ; 

int main(void)
{

	Banana a  , b , c , d , e , f , g;

	return 0 ; 
}

运行结果:

C++语言之静态变量的运用