static静态存储

来源:百度知道 编辑:UC知道 时间:2024/05/16 02:17:06
#include<iostream>
int Gather();
int main()
{
int gwool=0;
while(gwool<60){
gwool=Gather();
std::cout<<gwool<<std::endl;
}
return 0;
}
int Gather(){
static int wool=50;

return ++wool;
}

//static int wool=50 只在第一次调用时有用,如果去掉static,
//int wool=50 每次调用时都重新被初始化,则程序无法停止

连接时出错,何故?!?

把static int wool=50;这个语句放到
#include<iostream>后面定义试试.
可能是与变量的生存期有关系.

应该把静态去了吧,每次wool总为50,则gwool每次得到的附值是相同的,wool=5=,则++wool也为50,附值给gwool,为一个无限循环