Scope Resolution (::)
In this section of statements and expressions, we will take a look at the scope resolution operator (or double colons "::") that is new to C++. What it does is that it allows you to specify which scope to use rather than using the default scope (the local scope).
To use the global scope, you need to prefix the variable name with the scope resolution operator (::) and to use the local scope, you do not need to do anything to the variable name since by default, the scope is the local scope.
 | | ![]() | | |
int amount = 123; // global variable
void main()
{
int amount = 456; // local variable
cout << ::amount << endl; // Print the global variable (123)
cout << amount << endl; // Print the local variable (456)
} |
| ![]() |
|
Looking at the above code, there are two instances of the variable amount. One being in the global scope which contains the value 123 and the other in the local scope of the function main() which is initiated with a value of 456. The two colons tell the compiler to use the global amount instead of the local one.Please Rate this Code: