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
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
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.
::SendMessage()
denotes a method, that is available in the global namespace. It is not bound to the scope of any class in particular.