resolution operator

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.


you don't always need it.

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.
Last edited on
It's to ensure that the global SendMessage(HWND,UINT,WPARAM,LPARAM) is called and not another one that is closer in scope.

Consider:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

void f() { std::cout << "<global>::f()\n"; }

namespace ABC {
    void f() { std::cout << "ABC::f()\n"; }
    void g() {
        f();    // local
        ::f();  // global
    }
};

int main() {
    ABC::g();
}


Output:

ABC::f()
<global>::f()
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).
closed account (E0p9LyTq)
Scope and scope resolution can be a tricky subject to grasp.

A simple look at what scope means, and what the scope resolution operator does:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

const char i[ ] = "Global i";

int main()
{
   double i = 1.2;

   std::cout << "main scope i: " << i << '\n';
   std::cout << "global scope i: " << ::i << "\n\n";

   for (int i = 0; i < 3; i++)
   {
      std::cout << "\tfor loop scope i: " << i << '\n';
      std::cout << "\tglobal scope i: " << ::i << "\n\n";

      {
         char i = 'A';
         std::cout << "\t\tvery local scope i: " << i << '\n';
         std::cout << "\t\tglobal scope i: " << ::i << "\n\n";
      }
   }

   std::cout << "main scope i: " << i << '\n';
   std::cout << "global scope i: " << ::i << "\n\n";
}

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.
Thanks for all the replies giving me a better understanding of
scope that I was missing .

Last edited on
Topic archived. No new replies allowed.