0
What's the need of resolution scope ::
Where to place resolution scope
3 Respostas
+ 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.
+ 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.