What's the need of resolution scope :: | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What's the need of resolution scope ::

Where to place resolution scope

9th Sep 2020, 8:35 AM
Tanushree Tiwari
Tanushree Tiwari - avatar
2 Answers
+ 3
It is used to clarify where something came from. For example, you have two functions with the same name, but you declared them in different namespaces. To be clear about which function is to be called on the event of function invocation, you specify the namespace name, followed by the scope resolution operator, and then the function name. This will make it clear for the compiler, which of the functions having the same name is to be called. I think it has more use cases, but this is one I remember. Others will add more, just be patient.
9th Sep 2020, 9:23 AM
Ipang
+ 3
Consider the two classes: class A { public: static int func() {return 1;} }; class B { public: static int func() {return 2;} }; What will cout << func(); output? Hence here comes :: It lets both the compiler and the coder know what func() we are calling, and what class or namespace the func() belongs to. cout << A::func(); outputs 1. cout << B::func(); outputs 2.
9th Sep 2020, 9:33 AM
你知道規則,我也是
你知道規則,我也是 - avatar