假设这是一段代码,用于演示如何使用本地和全局变量.我试图向一个初级家伙解释这个,他问我这个问题.
在下面的代码中,你应该怎么做才能从外部循环中获取'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();
}
Run Code Online (Sandbox Code Playgroud)