The scope resolution operator (::) in C++ is used to define the already declared member functions (in the header file with the .hpp extension) of the class. In the .cpp file one can define the normal functions or the member functions of the class. To differentiate from the normal functions with the member functions of the class, one needs to use the scope resolution operator (::) in between the class name and the member function name i.e. ship::foo() where the ship is the class and the foo() is the member function in the ship. The other uses of the resolution operator is to resolve the scope of the variables if the same variable name is used for the global, local, and the data member of the class. If the resolution operator is placed between the class name and the data member belonging to the class then the data name belonging to the particular class is affected. If the resolution operator is placed in front of the variable name then the global variable is affected. If no resolution operator is placed then the local variable is affected.Scope Resolution Operator: ::
You can tell the compiler to use the global identifier rather than the local identifier by prefixing the identifier with ::, the scope resolution operator.
:: identifier
class-name :: identifier
namespace :: identifier //The identifier can be a variable or a function.
If you have nested local scopes, the scope resolution operator does not provide access to identifiers in the next outermost scope. It provides access to only the global identifiers.
This example has two variables named amount. The first is global and contains the value 123. The second is local to the main function. The scope resolution operator tells the compiler to use the global amount instead of the local one.
#include
using namespace std;
int amount = 123; // A global variable
int main() {
int amount = 456; // A local variable
cout << ::amount << endl // Print the global variable
<< amount << endl; // Print the local variable
}