[英]Difference between static variable inside and outside of a function?
static int count;
int main()
{
static int count;
}
在任何 function 内部和外部声明的 static 变量之间有什么区别吗?
(我的意思是scope 和变量计数的可见性)
您的第一个count
只能在模块中访问(该文件中的代码)。 您的第二个count
只能在main
中访问。
当您在方法之外声明时,所有人都可以使用 static 方法在其声明之后编写的函数。 在方法中声明 static 变量时,只能通过该方法访问。
全局变量的动态初始化也存在差异(请参见此处)。 总而言之,如果您有:
static int count = bar();
int main ()
{
static int count = foo ();
}
执行 main 时会调用 'foo',但标准 (C++ '03) 根本不需要调用 'bar'!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.