C++ Scope resolution operator


The scope resolution operator ( :: ) is used for several reasons. For example: If the global variable name is same as local variable name, the scope resolution operator will be used to call the global variable. It is also used to define a function outside the class and used to access the static variables of class.

Here an example of scope resolution operator in C++ language,

Example

 Live Demo

#include <iostream>
using namespace std;
char a = 'm';
static int b = 50;

int main() {
   char a = 's';

   cout << "The static variable : "<< ::b;
   cout << "\nThe local variable : " << a;
   cout << "\nThe global variable : " << ::a;

   return 0;
}

Output

Here is the output

The static variable : 50
The local variable : s
The global variable : m

Updated on: 25-Jun-2020

20K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements