[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;
}

这是一个例子

posted @ 2019-02-21 10:28  zengzhaocheng  阅读(944)  评论(0编辑  收藏  举报