Thread: :: operator

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    5

    :: operator

    Hi

    Can someone tell me what the :: does in front of API calls ?


    Code:
    ///////////////////////////////////////////////////////////////////////////
    // PUBLIC
    // Creates a popup window with a given width and height, and returns the result
    //
    // [in]  WNDPROC  : the callback function to receive messages for this application
    // [in]  HINSTANCE: the instance of the application
    // [in]  iInitShow: the way to show a window for the first time
    // [in]  iWidth   : the requested width (before adjusting and centering)
    // [in]  iHeight  : the requested height (before adjusting and centering)
    //
    // [retval] TRUE if ok, FALSE if not
    /////////////////////////////////////////////////////////////////////////////
    BOOL CApplication::initPopup (WNDPROC wndProc, HINSTANCE hInst, int iInitShow, int iWidth, int iHeight)
    {
      WNDCLASS    wc;
      //  Set up and register window class
      wc.style = CS_HREDRAW | CS_VREDRAW ;
      wc.lpfnWndProc = wndProc ;
      wc.cbClsExtra = 0 ;
      wc.cbWndExtra = 0 ;
      wc.hInstance = hInst ;
      wc.hIcon = ::LoadIcon (hInst, IDI_APPLICATION) ;
      wc.hCursor = ::LoadCursor (NULL, IDC_ARROW) ;
      wc.hbrBackground = (HBRUSH) ::GetStockObject (BLACK_BRUSH) ;
      wc.lpszMenuName = m_p_chName ;
      wc.lpszClassName = m_p_chName ;
      ::RegisterClass (&wc);
      // Create a fullscreen window
      m_hwnd = ::CreateWindowEx (0,
            m_p_chName,
            m_p_chName,
            WS_POPUP,
            0, 0, iWidth, iHeight,
            NULL,
            NULL,
            hInst,
            NULL ) ;
      if (!m_hwnd)
      {
         return FALSE ;
      }
      m_hinst = hInst ;
      ::ShowWindow (m_hwnd, iInitShow) ;
      m_enumMode = FULLSCREEN ;
      //
      return TRUE ;
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    it just says they're global
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    5
    Hi,

    Can you give me more information ?
    By global you mean, not a member function of the class ?
    BTW, I know what a global variable is, but I have never seen a
    global function.

    Thanks
    Last edited by Jurgen; 03-27-2005 at 09:15 AM.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    :: is the scope resolution operator. Used by itself, it pertains to an object (function/value) at the global scope as oppossed to the local scope. For example:

    Code:
    #include <iostream>
    
    namespace bob
    {
        int x = 15;
    };
    
    int x = 10;
    
    int main()
    {
        int x = 5;
    
        std::cout << x << ' ' << ::x << ' ' << bob::x << std::endl;
    
        return 0;
    }
    Output:
    Code:
    5 10 15
    By itself, outputting x gives the value of 5 since this is the value stored by the local integer x. When you print ::x however, it outputs the value of the x that was declared globally which is 10. And of course, printing bob::x causes the value 15 to be printed because that was the value stored in the x within the "bob" scope.

    When used with functions, it doesn't really seem to serve much purpose in my opinion. Maybe someone else can explain it better. All non-static functions are global functions I believe. They can be called either with the :: or without it as follows:

    Code:
    #include <iostream>
    
    void happy()
    {
        std::cout << "I am happy!" << std::endl;
    }
    
    int main()
    {
        ::happy();
    
        happy();
    
        return 0;
    }
    Output:
    Code:
    I am happy!
    I am happy!
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    5
    Wow,

    I appreciate it, thanks for the explanation.

    Regards

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    One of the most obvious uses would be if a more local function/method has the same name as a global function:
    Code:
    class Window
    {
      public:
        ShowWindow(...);
    
      ...
    };
    
    Window::ShowWindow(...)
    {
      ::ShowWindow(...);
    }
    If the :: is left out in the call above you'll call the class method, not the global function.
    (I've done this mistake before :P )
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    When used with functions, it doesn't really seem to serve much purpose in my opinion. Maybe someone else can explain it better. All non-static functions are global functions I believe.
    Static member functions in classes?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by Magos
    Static member functions in classes?

    No, I was talking about regular ol' static functions... the ones that only have file scope, i.e.:

    Code:
    //file1.cpp
    #include <iostream>
    
    void foo1()
    {
        std::cout << "foo1: Hello!" << std::endl;
    }
    
    static void foo2()
    {
        std::cout << "foo2: Hello!" << std::endl;
    }
    
    
    //main.cpp
    
    void foo1();
    void foo2();
    
    int main()
    {
        foo1();    // OK since foo1 is not static
        ::foo1();  // OK since foo1 is not static
    
        foo2();    // Not OK, causes linker error
        ::foo2();  // Not OK, causes linker error
    
        return 0;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    I just thought of a case where you would need to use :: in front of a function. It occurs when you have a namespace where a certain function exists and you did a using namespace ...; statement to bring that whole namespace into the global namespace. If you also had a function already in the global namespace with the same name/return argument/parameters then when calling the function you would need to explicitly state the scope as either the global one or the one from the namespace we opened up. As an example:

    Code:
    #include <iostream>
    
    namespace bob
    {
        void whatever()
        {
            std::cout << "bob: whatever" << std::endl;
        }
    };
    
    void whatever()
    {
        std::cout << "global: whatever" << std::endl;
    }
    
    // Bring "bob's" stuff into the global namespace, now there
    // are two "whatever" functions in the global namespace
    using namespace bob;
    
    int main()
    {
        whatever();       // Error, ambiguous... is it the global one or the one in "bob"
    
        ::whatever();     // Calls the global "whatever" function
    
        bob::whatever();  // Needed to call the "bob" version of function
                          // even though we have already brought "bob"
                          // into the global namespace
    
        return 0;
    }
    So I guess this is one reason why simply doing a using namespace ...; statement to import everything into the global namespace can lead to some confusion.

    [edit]Maybe this is the same thing Magos was talking about...[/edit]
    Last edited by hk_mp5kpdw; 03-27-2005 at 02:38 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  10. #10
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Maybe this is the same thing Magos was talking about...
    Sort of, I meant calling from within another class method, but the principle is the same.
    Code:
    void Func()
    {
    }
    
    class Junk
    {
      static void Func()
      {
      }
    
      static void Momma()
      {
        Func();
        ::Func();
      }
    };
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Failure to overload operator delete
    By Elysia in forum C++ Programming
    Replies: 16
    Last Post: 07-10-2008, 01:23 PM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 08:38 AM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM