1

I've learned that "::" is a scope resolution operator and am starting to understand its use.

However, I saw "::" used without a prefix in some code. I've never seen that before. For example:

::memcopy();
::GetKeyBoardState();

Just curious on the syntax and need for the "::" to be used in the code. I removed the "::" and Intellisense does not red underline it so was wondering what was going on. Thanks

  • To make sure it is called from no namespace, I guess. Else somebody might write `using namespace ...;` which might have functions with the same name and it might result in ambiguity. – ALX23z Mar 29 '20 at 23:03
  • Sidenote: Intellisense will give you a rough estimate of what the outcome will be if you compile. Actually compiling often gives a totally different result than what Intellisense showed. – Ted Lyngmo Mar 29 '20 at 23:06

1 Answers1

3

A qualified name that begins with :: instead of a scope name is a fully qualified name. This means that the name is absolute, and therefore does not perform a relative name lookup.

For example:

void GetKeyBoardState();           // a

namespace foo {
    void GetKeyBoardState();       // b

    void bar() {
        ::GetKeyBoardState();      // calls a
        GetKeyBoardState();        // calls b
        foo::GetKeyBoardState();   // calls b
        ::foo::GetKeyBoardState(); // calls b
    }
}

void bar() {
    ::GetKeyBoardState();          // calls a
    GetKeyBoardState();            // calls a
    foo::GetKeyBoardState();       // calls b
    ::foo::GetKeyBoardState();     // calls b
}

This is similar to how absolute and relative paths work in filesystems as well as fully qualified domain names vs hostanames.

eerorika
  • 232,697
  • 12
  • 197
  • 326