注册 登录
编程论坛 C++教室

关于static

cclearner 发布于 2007-06-28 23:03, 485 次点击
局部静态变量,用static 定义的,在局部,和主函数中怎么起作用
比方说
int main()
{
static int a=2;
...
}
void other(void)
{
static int a=3;
a=a+3;
...
}
是否 other 中的a ,回到主函数中就不算了,再次回到other 中时保留?
5 回复
#2
天下第二刀2007-06-29 01:30

#3
smithallen2007-06-29 09:41
局部静态变量,在所在函数结束时,仍然保留。

但对于其他的函数而言,局部静态变量是不可见的。
#4
aipb20072007-06-29 13:28
自己写段代码测试不就清楚了?
#5
cclearner2007-06-29 22:45
我是新手啊,试着写了点东西,还是不确定到底怎么回事,debug里的东西不会用啊,谢谢大家了!
#6
yushui2007-06-30 08:17
#include<iostream.h>
int other(void);
void main()
{
static int a=2;
for(int i=1 ;i<5;i++)
{ cout<<other()<<endl;}
cout<<endl<<a<<endl;
}
int other(void)
{
static int a=2;
a++;
return a;
}
这个例子
1