【发布时间】:2018-07-27 01:25:57
【问题描述】:
可以使用 :: 运算符在函数内访问全局范围的变量。由于全局范围没有名称,因此 :: 的左侧可能为空。我将如何访问在函数范围中定义的变量,该变量稍后在函数本身的块中被覆盖。在下面的代码中,我将如何访问初始化为 1 的变量?
extern int reused = 0;
int main()
{
int reused = 1;
{
int reused = 2;
cout << reused << endl; // how to get the reused inited to 1 here?
}
}
【问题讨论】:
-
也许function.reused?
-
也许通过使用不同的变量名?
-
你不能。就这么简单。
-
我原以为你会收到有关阴影的警告
标签: c++ scope-resolution