+ 1

scope resolution operator

hi , I'm currently trying to gain a better understanding on the use of the scope resolution operator (::). its my understanding that, when a class as been declared, a member or object of that class can be called using the (.) operator. However, I have seen the (::) being used for the same purpose. Any clarification on the purpose of the (::) and the difference between the uses of both (.) and (::) would be greatly appreciated. Thanks

16th Nov 2016, 1:36 PM
Tony Robinson
Tony Robinson - avatar
2 Antworten
+ 1
scope resolution operator(::) is mainly used for accessing global variable. For example : int a=5; int main() { int a=10; cout<<a; //10 cout<<::a; //5 return 0; } hope it explains.
27th Nov 2016, 9:21 AM
Rishabh Agrawal
Rishabh Agrawal - avatar
0
Would it be a case that, If I had a class A (see below) and wanted to use an object or member of that class within main, I would use the (::) operator to tell the complier that the member I want to access is within class A. Therefore, A::somefunction(); ? ie. class A { public: some_function(); }; int main() { A::some_function(); } In such as case, even though the class members and objects are public, how come they do not operate as global functions and members? Also, could I access the member by saying, A.somefunction(); ?
16th Nov 2016, 10:34 AM
Tony Robinson
Tony Robinson - avatar