+ 2

What is purpose of scope resolution operator?

Also abbreviated as (::) what is its purpose when dealing with object oriented programming

23rd Feb 2018, 12:06 PM
Andrew Mwendwa
Andrew Mwendwa - avatar
4 Answers
+ 2
You'll mostly need it for header files. For example class declaration in file.h, but the implementation in file.cpp. To implement the method you need the scope resolution operator.
23rd Feb 2018, 12:20 PM
Alex
Alex - avatar
+ 1
//You can use it to access global variables in this scenario #include <iostream> using namespace std; int x=9; //global variable int main() { int x=5; //local variable with same name but different scope cout<<x<<endl; //returns local variable, 5 cout<<::x<<endl; //returns global variable, 9 return 0; } //hope that helps
14th Aug 2018, 3:02 PM
Moses Odhiambo
Moses Odhiambo - avatar
+ 1
You can also use the scope resolution operator to access a class' static members from outside the class, #include <iostream> using namespace std; class X { public: static int count; }; int X::count = 10; // define static data member int main () { int X = 0; // hides class type X cout << X::count << endl; // use static member of class X } This will give the output: 10
14th Aug 2018, 3:09 PM
Moses Odhiambo
Moses Odhiambo - avatar