[C++]局部静态变量在递归中只被初始化一次

#include <iostream>

using namespace std;

void foo()
{
    static int count = 0;

    if(count<5)
    {
        count++;
        cout<<count<<endl;
        foo();
    }
    else
    {
        cout<<"count > 5"<<endl;
    }
}

int main()
{
    foo();  //increment count from 0 to 5
    foo();  //count is already at 5

    return 0;
}

这是一个例子

猜你喜欢

转载自www.cnblogs.com/drunknbeard/p/10410818.html
今日推荐