Selected Reading

Scope Resolution Operator in C++



In C++, scope resolution operator is used to define a function outside the class and access the static variables of a class. It accesses the identifiers such as classes and functions and is denoted by double colon (::).

Here is the syntax of a scope resolution operator −

scope_name :: identifier

Here,

  • scope − It can be a class name, namespace, or, global.
  • identifier − The identifier can be a variable, function, type, or, constant.

Read this chapter to get a better understanding of various applications of scope resolution operator.

Application of Scope Resolution Operator

The scope resolution operator is used for multiple purposes that are mentioned below −

namespace Resolution

A namespace is used to differentiate similar functions, classes, variables, etc., with the same name available in different libraries. To access members which are defined inside a namespace, we use the scope resolution operator '::'.

In this example, we have used the scope resolution operator with cout to print the message.

#include <iostream>
int main() {
   std::cout << "This message is printed using std::" 
             << std::endl;
}

The output of the above code is as follows:

This message is printed using std::

Defining a Function Outside Class

To define a function outside the class, we use the scope resolution operator. It is used to differentiate a global function from a function that belongs to a class. Here is an example −

In this example, we have declared a function display in the class Example. To define this function outside the class, we have used the scope resolution operator. This function displays the value of the num.

#include <iostream>
using namespace std;
class Example {
   int num = 10;
public:
   void display();
};
void Example::display() // Defining Function outside class
{
   cout << "The value of num is: " << num;
}
int main(){
   Example obj;
   obj.display();
   return 0;
}

The output of the above code is as follows −

The value of num is: 10

Accessing Global Variable

To access a global variable that is already locally present in a function, we use the scope resolution operator with the global variable.

The following example prints the value stored inside the variable num. The 'num' variable is used as local as well as a global variable, and we have used the scope resolution operator to access the global variable.

#include<iostream>
using namespace std;
int num = 7;
int main() {
   int num = 3;
   cout << "Value of local variable num is: " << num;
   cout << "\nValue of global variable num is: " << ::num;
   return 0;
}

The output of the above code is as follows −

Value of local variable num is: 3
Value of global variable num is: 7

Accessing static Member Variables

To access or define a static member variable, we use a scope resolution operator. The static members belongs to class and are object-independent so they must be defined outside the class. They can be accessed using a class name with the help of the scope resolution operator.