In C++, you can tell the compiler to use the global variable rather than the local variable by prefixing the variable with ::, the scope resolution operator.
If you have nested local scopes, the scope resolution operator doesn't provide access to variables in the next outermost scope. It provides access to only the global variables.
Example
// Example of the scope resolution operator
#include <iostream.h>
int amount = 123; // A global variable
void main()
{
int amount = 456; // A local variable
cout << ::amount; // Print the global variable
cout << '\n';
cout << amount; // Print the local variable
}
The example has two variables named amount
. The first is global and contains the value 123. The second is local to the main function. The two colons tell the compiler to use the global amount
instead of the local one.