C++ Scope resolution operator

C++ProgrammingServer Side Programming

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
raja
Published on 24-Oct-2018 14:32:25
Advertisements