I am new to C++ and have seen the following code fragment in numerous
places and have been unable to make sense of it or find any source
on the net to explain.
My problem is with the scope resolution operator -- '::'.
::SendMessage(...);
I understand what the scope resolution operator is and why it is used .
I don't understand this abbreviated notation.
It might make more sense if I was using a 'using' statement -- but I am not.
If I was using a 'using' statement , I wouldn't expect to see the '::'.
It compiles with and without the '::' -- why
thanks.
if you do not use namespace std which is recommended (that is, not using it is recommended) you see a lot of std::cout type code.
classes use it a lot for their methods and its required.
it can be needed if you have 2 variables of the same name in 2 different namespaces.
it can be defensive. if you use std::cout, even with the namespace, if someone made their own local cout function it will still be correct.
At the most simple way to explain it, the compiler will tell you when you need it because it won't compile without it when it does.
At the most simple way to explain it, the compiler will tell you when you need it because it won't compile without it when it does.
It actually can still compile. That's the problem! If there's a matching function in-scope then it will be called instead of the intended one (just like you said with cout).
main scope i: 1.2
global scope i: Global i
for loop scope i: 0
global scope i: Global i
very local scope i: A
global scope i: Global i
for loop scope i: 1
global scope i: Global i
very local scope i: A
global scope i: Global i
for loop scope i: 2
global scope i: Global i
very local scope i: A
global scope i: Global i
main scope i: 1.2
global scope i: Global i
Four different i variables created, each of a different type. Yet the compiler knows which one is which based on the context (the scope), with some help using the scope resolution operator.