The Scope Resolution Operator consists of two colons :: and is used to specify which named context area, an identifier (variable, etc) is being referred to.
The named context area might be a function, class, struct, namespace.
For instance a class may have a number of members contained within its body code block, which can be referred to using the scope resolution operator, like so:
className :: member ;
If the className is omitted, it is assumed that the program's Global scope is being referred to, like so:
:: member ;
This example creates a global function, a namespace and a class all with unique returned values:
#include <iostream> using namespace std; int getValue(){ return 21 ; } namespace mySpace { int getValue(){ return 42 ; } }; class myClass { public: static int getValue(){ return 17 ; } }; int main() { cout << "Global using :: to refer to getValue() = " << ::getValue() << endl ; cout << "Global no scope resolution, getValue() = " << getValue() << endl << endl ; cout << "Namespace referring to getValue() = " << mySpace::getValue() << endl << endl ; cout << "Now instantiating a myClass object" << endl ; myClass* ptr = new myClass() ; myClass myObject ; cout << "Class using -> to refer to getValue() = " << ptr->getValue() << endl ; cout << "Class using . member access to refer to getValue() = " << myObject.getValue() << endl ; cout << "Class using :: to refer to getValue() = " << myClass::getValue() << endl ; return 0; }
Compile & Run:
Global using :: to refer to getValue() = 21 Global no scope resolution, getValue() = 21
Namespace referring to getValue() = 42
Now instantiating a myClass object |