当前位置:网站首页>static 函数中的静态变量
static 函数中的静态变量
2022-07-02 07:19:00 【luoganttcc】
函数中的静态变量
当变量声明为static时,空间将在程序的生命周期内分配。即使多次调用该函数,静态变量的空间也只分配一次,前一次调用中的变量值通过下一次函数调用传递。这对于在C / C ++或需要存储先前函数状态的任何其他应用程序非常有用。
#include <iostream>
#include <string>
using namespace std;
void demo()
{
// static variable
static int count = 0;
cout << count << " ";
// value is updated and
// will be carried to next
// function calls
count++;
}
int main()
{
for (int i=0; i<5; i++)
demo();
return 0;
}
输出:
0 1 2 3 4
边栏推荐
猜你喜欢
随机推荐
Sus system availability scale
01-spooldir
Leetcode+ 76 - 80 storm search topic
华为应用市场应用统计数据问题大揭秘
Pywin32打开指定窗口
C#中索引器
MYSQL关键字
Point cloud projection picture
JSP webshell free -- webshell free
MongoDB-快速上手MongoDB命令行的一些简单操作
P1055 [noip2008 popularization group] ISBN number
JSP webshell free -- the basis of JSP
华为游戏初始化init失败,返回错误码907135000
MongoDB 学习整理(条件操作符,$type 操作符,limit()方法,skip() 方法 和 sort() 方法)
PCL 从一个点云中提取一个子集
12. Process synchronization and semaphore
JSP webshell免杀——JSP的基础
UVM learning - object attribute of UVM phase
Hdu1228 a + B (map mapping)
shell编程01_Shell基础