• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 265
  • Last Modified:

Scope operator

What is the meaning of the scope operator before LoadBitmap in the code below...

m_button.SetBitmap(::LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_GREEN)));

m_button is a CButton object assigned to an MFC dialog button. The goal here is to put the IDB_GREEN bitmap onto the button, and this code does do that, but the :: is not clear.  I understand that normally you would use ::  if you wanted to specify the class, on the left side,   that the method, on the right side,  is to come from, but the left side of this :: is not a class, it's a method of the CButton class. Curiously, the code works fine without the :: and I only put it in because the sample code I got this from had it.

Thanks for any thoughts,
1 Solution
>>the :: is not clear

::LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_GREEN))

instucts the compiler to explicitly choose the Win32 API 'LoadBitmap()' instead of 'CBitmap::LoadBitmap()', that's why the scope operator is being used - to avoid a potential naming conflict with that method.
stevaAuthor Commented:
Ok, but how does ::LoadBitmap instruct the compiler to use the Win32 version?  From everything I've read, :: separates a class on the left from a method on the right.  What is the meaning, in general, when there's no class on the left?
in general :: instructs compiler get method from GLOBAL namespace.
:: is a scope resolution operator. Often it is used to select the class scope. A different example is the use of cout. If you don't include the line
using namespace std;
you need to specify the namespace of cout for using it:
std::cout << "test" << std::endl;

if no namespace is given infront of this operator the global namespace is used. So ::LoadBitmap used the function from the global namespace whereas LoadBitmap inside a class function will take the corresponding member function of that class (or of any base it is derived when it is accessible).

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now