:: scope-resolution-operator-in-C++ .

In this session i have discuss :: scope resolution operator in C++ .


Like C , C++ is also block structured language . We know that the same variable in different block have different meaning . The scope depends on their block .

In C global variable can not be accessd from or with in inner block . C++ introduced a new operator called :: scope resolution operator to solve this problem . You can call the global version using :: scope resolution operator .


:: Scope Resolution Operator Demo

Example : Write a program that demonstrate the :: scope resolution operator .

    #include<iostream>
    using namespace std;
    int s=5;
    int main()
    {
        int s=20; // s redeclared to main
        {
            int k=s;
            int s=10; // s declared again local to inner block
            cout<<"\n We are in inner block \n";
            cout<<"k="<<k<<"\n";
            cout<<"s="<<s<<"\n";
            cout<<"::s="<<::s<<"\n";
       }
       cout<<"\n We are in outer block\n";
       cout<<"s="<<s<<"\n";
       cout<<"::s="<<::s<<"\n";
       return 0;
    }
    
    OUTPUT : We are in inner block
             k=20
             s=10
             ::s=5
             We are in outer block
             s=20
             ::s=5
    

Welcome to AndroidATPoint.

back image                     next image