static 函数中的静态变量

函数中的静态变量
当变量声明为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 
posted @ 2022-08-19 22:42  luoganttcc  阅读(2)  评论(0编辑  收藏  举报