1. To access a global variable when there is a local variable with the same name
int count = 0;
int main(void) {
int count = 0;
::count = 1; // set global count to 1
count = 2; // set local count to 2
return 0;
}
The declaration of count declared in the main function hides the integer named count declared in global namespace scope. The statement ::count = 1 accesses the variable named count declared in global namespace scope.
2. You can also use the class scope operator to qualify class names or class member names. If a class member name is hidden, you can use it by qualifying it with its class name and the class scope operator.
#include <iostream>
using namespace std;
class X
{
public:
static int count;
};
int X::count = 10; // define static data member
int main ()
{
int X = 0; // hides class type X
cout << X::count << endl; // use static member of class X
}
the declaration of the variable X hides the class type X, but you can still use the static class member count by qualifying it with the class type X and the scope resolution operator.
1. https://www.geeksforgeeks.org/scope-resolution-operator-in-c/
2. https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.3.0/com.ibm.zos.v2r3.cbclx01/cplr175.htm