Scope Resolution Operator in C++

Video Tutorial
FREE
Operators Introduction thumbnail
This video belongs to
C++ Course: Learn the Essentials
14 modules
Certificate
Topics Covered

The scope resolution operator (::) in C++ allows access to global variables or functions that are overshadowed by local ones. It resolves conflicts when both global and local variables or functions share the same name. By prefixing the operator to the variable or function name, programmers can specifically refer to the global entity within the program, ensuring its visibility and usage.

Uses of Scope Resolution Operator in C++

Following are some uses of scope resolution operator in C++:

  1. It is used to access the hidden variables, global variables, class members, and members of the nested classes.
  2. You can place class member functions outside the class using the scope resolution operator.
  3. It is used to access the static variables and static functions of a class.
  4. It is also used to access variables of the same name from different namespaces.

Program to access the hidden value using the scope resolution (::) operator

This is one of the basic use of scope resolution operator in C++ explained in the first example in the introduction, where we are given two variables of the same name, one in the local scope and another in the global scope. We have to access the global variable inside the local scope to perform operations like assigning/reassigning some value, initialization, or any other operation like arithmetic, bitwise, etc. we postfix the variable with scope resolution operator :: to access that global variable in all expression wherever it is used.

For example, in the following code, the variables named size are declared globally as well as locally, and the rest of the operations on the global variable size are performed by post-fixing the size variable with scope resolution operator :: as follows:

Syntax:

Code:

Output:

This is how the name of the identifier is resolved from another scope using the scope resolution operator.

Program to define the member function outside of the class using the scope resolution (::) operator

In object-oriented programming, when we create a class that has some member functions which work on the data members of the class, we can declare the functions in the class. Those functions can be defined outside the class using the scope resolution operator in c++:

Syntax:

Let's see an example:

Output:

Program to demonstrate the standard namespace using the scope resolution (::) operator

In a single scope, we can create only a single variable with a given name as creating another variable of the same name will give an error, but we can create different variables and functions with the same name using namespaces, and to access those variables, we use scope resolution operator for example:

Syntax:

Let's implement the same using the scope resolution operator in C++.

Output:

Here different scopes are accessed by the scope resolution operator. As we can see, val in the first and second namespaces are accessed using the scope resolution operator. In the second line of the program, we write "using namespace std", it simply means that we are using namespace std, i.e., standard it means we can use all functions and variables in the std namespace, it contains all the standard functions, variables, streams for example cout, cin, endl, etc.. We declare them as standard namespace so that we can use them without using the keyword std again and again, like std:endl, std:cout, std:cin etc.

std namespace is written as

Hence, we can use those functions and variables from std.

Program to access the static variable using the scope resolution (::) operator

In a class, the static members are declared along with other variables. To access those static variables to assign values to them and perform other operations scope resolution operator is used where static variables are accessed using the syntax given below:

Syntax:

Let's see an example to understand it, and we shall implement this use case of scope resolution operator in c++.

Output:

We know that static variables are attached to a class and not to the objects of that class, and they are accessed using the scope resolution operator in any function scope. As we did in the example above, they can also be accessed inside the main function.

Program to access the static member function using the scope resolution (::) operator

Code:

Output:

Program to override the member function using the scope resolution (::) operator

Code:

Output:

Conclusion

  1. We have seen what is scope resolution operator in C++. The scope resolution operator (::) in C++ enables access to global variables or functions overshadowed by local ones.
  2. It resolves conflicts when both global and local entities share the same name, allowing specific reference to the global entity.
  3. It's used to access hidden variables, global variables, class members, and members of nested classes.
  4. You can place class member functions outside the class using the scope resolution operator.
  5. It provides access to static variables and functions of a class.
  6. Additionally, it facilitates access to variables of the same name from different namespaces, ensuring clarity and avoiding naming conflicts.