静态变量例子

// test100.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include <iostream>
using namespace std;

// Function containing static variables
// memory is retained during execution
int staticFun()
{
	cout << "For static variables: ";
	static int count = 0;
	count++;
	return count;
}

// Function containing non-static variables
// memory is destroyed
int nonStaticFun()
{
	cout << "For Non-Static variables: ";

	int count = 0;
	count++;
	return count;
}

int main()
{

	// Calling the static parts
	cout << staticFun() << "\n";
	cout << staticFun() << "\n";
	;

	// Calling the non-static parts

	cout << nonStaticFun() << "\n";
	;
	cout << nonStaticFun() << "\n";
	;

	system("PAUSE");
	return 0;
}

 

For static variables: 1
For static variables: 2
For Non-Static variables: 1
For Non-Static variables: 1
请按任意键继续. . .

 

posted @ 2021-10-05 14:50  xmydis  阅读(80)  评论(0)    收藏  举报