How to access a global variable within a local scope?
问题 This is my code #include <iostream> using namespace std; int x = 5; int main() { int x = 1; cout << "The variable x: " << x << endl; } I get as output 1 , but I would like to have 5 , as in accessing the global x variable. Is this possible? 回答1: You should be using ::x in order to access global variable in local scope. The operator :: is unary scope resolution operator. So your code should be: #include <iostream> using namespace std; int x = 5; int main() { int x = 1; cout << "The variable x: