Scope operator

Posted on 2004-11-24
Last Modified: 2013-11-20
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,
Question by:steva
    LVL 86

    Expert Comment

    >>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.

    Author Comment

    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?
    LVL 12

    Expert Comment

    in general :: instructs compiler get method from GLOBAL namespace.
    LVL 13

    Accepted Solution

    :: 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

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    configuration management 2 96
    iSeries DB2 Query 2 69
    Detect CR LF to each line 12 117
    VBA "SendKeys" Syntax for Multiple Keystrokes 7 87
    In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
    Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now