void foo() {
static int x;
}
void bar() {
static int x;
}
int main() {
foo();
bar();
}
最佳答案
他们每个人都只看到自己的。无法从 scope 之外“看到”变量它在中声明。
另一方面,如果您这样做:
static int x;
void foo() {
static int x;
}
int main() {
foo();
}
然后 foo()
只看到它的本地 x
;全局 x
已被它“隐藏”。但是对一个的更改不会影响另一个的值。
关于c++ - 在 C++ 中,如果两个不同的函数声明同一个静态变量会怎样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4108147/