![](/img/trans.png)
[英]Is a local scoped variable initialized to an undetermined value, or un-initialized?
[英]Get the middle scoped value of variable
假设这是一段代码,以演示如何使用局部和全局变量。 我试图向一个小伙子解释这个问题,他问了我这个问题。
在下面的代码中,您应该怎么做才能从外循环中获取“ x”的值。 在这种情况下,如何访问值为2的“ x”。
#include<iostream>
using namespace std;
int x = 1;
void fun() {
int x = 2;
{
int x = 3;
cout << x << endl; // This will give 3
cout << ::x << endl; // This will give 1
// What should I write here to get x = 2.
}
}
int main() {
fun();
}
你不能 第一个x
的名称已被第二个x
隐藏。 由于第一个x
不属于名称空间(包括全局名称空间),类或枚举,因此您无法限定其名称,因此无法访问。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.