Use of the unary scope resolution operator::

 What is the purpose of the unary scope resolution operator::
 1. Act on a variable
 2. When the local variable and the global variable have the same name, access the global variable in the scope where the local variable is located

Below I will use two examples for comparison;

Example 1:

int x=5;
int main()
{
	int x=0;
	cout << x;
	return 0;
}

The output here is 0; it means that when the global variable and the local variable have the same name, the local variable has a higher priority and the global variable is blocked.

Example 2:

To call a global variable with the same name in a local variable scope, use the unary scope resolution operator::

int x=5;
int main()
{
	int x=0;
	cout << ::x;
	return 0;
}

The output here is 5;

Seeing this, you can clearly understand the role of ::.

Guess you like

Origin blog.csdn.net/weixin_74287172/article/details/130572549