1

I started using MFC a while ago and I came across functions or rather methods, that are called with "::" before name of the functions. What is it?

For example:

::SendMessage()

What is that scope? How can I define one if I wanted to?

Thanks

ks1322
  • 33,961
  • 14
  • 109
  • 164
ligazetom
  • 83
  • 1
  • 8

3 Answers3

4

The :: before a function name specifies to use that function from the global namespace rather than a member function of a particular class. This, as you have found, is used extensively in the MFC headers/source.

The reason is actually quite simple. Let's consider the CWnd class, which has a SendMessage member function with the following signature:

class CWnd : public CObject {
//...
    LRESULT SendMessage(UINT msg, WPARAM wParam, LPARAM lParam);
//...
}

The implementation of this function can simply pass control to the non-MFC, global WinAPI function with the same name. But this has a different signature:

LRESULT SendMessage(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);

So, when the member function calls this non-MFC object, with the first argument being its 'own' HWND object, the :: prefix is used to make it clear to any future coders that a non-MFC function is being called:

LRESULT CWnd::SendMessage(UINT msg, WPARAM wParam, LPARAM lParam)
{
    return ::SendMessage(m_hWnd, msg, wParam, lParam); // Clarifies that we're using a non-class function.
}

Feel free to ask for further explanation and/or clarification.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • 2
    Using a fully qualified name is not required here. There is no risk of a recursive call. The class member has a different signature, so it's discarded as a candidate during [overload resolution](https://en.cppreference.com/w/cpp/language/overload_resolution). [Unqualified name lookup](https://en.cppreference.com/w/cpp/language/unqualified_lookup) will find names in global namespace. – IInspectable Dec 10 '19 at 16:31
  • 1
    @IInspectable Indeed, yet the MFC source does use the *explicit* global namespace prefix. Makes things much clearer, I feel. (Post edited for correctness!) – Adrian Mole Dec 10 '19 at 16:33
  • 1
    Personally, I would *always* be explicit. Using a fully qualified name more directly expresses the intent, reduces mental load for readers, and safeguards against future changes, that might introduce names that would eventually collide. It's just not *"required"*. – IInspectable Dec 10 '19 at 16:39
  • And how would I define global namespace for myself? Like if I wanted to add method to global namespace, or creating Empty project, where would I start ? – ligazetom Dec 11 '19 at 17:22
  • @ligazetom To add to the global namespace, just define a global function - that is, a function that isn't in any other namespace or class, like a good, old-fashioned `extern int MyFunction(int arg);` on its own, somewhere. – Adrian Mole Dec 11 '19 at 17:25
3

:: is refering to the global namespace.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
2

::SendMessage() denotes a method, that is available in the global namespace. It is not bound to the scope of any class in particular.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70