Header Ads Widget

Scope Resolution Operator in C++

:: is scope resolution operator. Whenever there is a conflict between local and global variables, the local variable defined inside function gets the priority. Moreover the global variable is inaccessible when a local variable of same name is available within the function. C++ allows you the flexibility of accessing both the variables. It achieves this through scope resolution operator. Using this we can access a global variable and print its existing value and we can also reinitialize it to another value.  

#include <iostream>
using namespace std;
int a=10;
int main( )
{
    int a=15;
    cout << "Local value of a =" <<a <<"Global value of a ="<< ::a ;
    ::a = 50;
    cout << "Local value of a =" <<a <<"Global value of a ="<< ::a ;
    return 0;
}

Post a Comment

2 Comments