:: before function calls

Discussion in 'C++' started by David Blasdell, Jul 29, 2004.

  1. I have noticed (especially in multi-thread examples) :: being but before
    function names, however I'm unsure what difference this makes.

    Examples:
    ::Sleep(0);
    hThread = ::CreateThread(NULL, 0, ThreadFunc, (LPVOID)kk, NULL, &dwID);

    What difference does it make, if any?


    Thanks
    David Blasdell, Jul 29, 2004
    #1
    1. Advertising

  2. "David Blasdell" <> wrote in message
    news:ceakca$785$...
    > I have noticed (especially in multi-thread examples) :: being but before
    > function names, however I'm unsure what difference this makes.
    >
    > Examples:
    > ::Sleep(0);
    > hThread = ::CreateThread(NULL, 0, ThreadFunc, (LPVOID)kk, NULL, &dwID);
    >
    > What difference does it make, if any?
    >


    It means that the function Sleep and CreateThread will be looked up by the
    compiler in the global namespace, so there will be no confusion with any
    other functions with the same names in any other namespace. For instance

    void dubious()
    {
    cout << "global\n";
    }

    namespace silly
    {
    void dubious()
    {
    cout << "silly\n";
    }

    void sod()
    {
    ::func();
    }
    }

    int main()
    {
    silly::sod();
    }

    By saying ::dubious() in the function sod I am making sure that the dubious
    function in the global namespace is being called not the dubious function in
    the silly namespace.

    This has nothing to do with threading, and I would guess that you see it in
    threading code because of slavish copying of code by programmers who don't
    understand the reasons for what they are copying.

    john
    John Harrison, Jul 29, 2004
    #2
    1. Advertising

  3. >
    > void sod()
    > {
    > ::func();
    > }
    > }
    >


    Typo should be

    void sod()
    {
    ::dubious();
    }

    john
    John Harrison, Jul 29, 2004
    #3
  4. Thanks, I understand now :)

    David

    "John Harrison" <> wrote in message
    news:...
    > >
    > > void sod()
    > > {
    > > ::func();
    > > }
    > > }
    > >

    >
    > Typo should be
    >
    > void sod()
    > {
    > ::dubious();
    > }
    >
    > john
    >
    >
    David Blasdell, Jul 29, 2004
    #4
  5. David Blasdell

    JKop Guest

    David Blasdell posted:

    > Thanks, I understand now :)
    >
    > David
    >
    > "John Harrison" <> wrote in

    message
    > news:...
    >> >
    >> > void sod()
    >> > {
    >> > ::func(); } }
    >> >

    >>
    >> Typo should be
    >>
    >> void sod()
    >> {
    >> ::dubious(); }
    >>
    >> john
    >>
    >>

    >
    >
    >


    Also, for variables:

    int k = 42;

    void func()
    {
    double k = 76.22;

    ::k = 7;

    }


    -JKop
    JKop, Jul 29, 2004
    #5
  6. David Blasdell

    JKop Guest

    > Also, for variables:
    >
    > int k = 42;
    >
    > void func()
    > {
    > double k = 76.22;
    >
    > ::k = 7;
    >
    > }
    >
    >
    > -JKop


    Just to expand on that further:

    namespace Chocolate {
    unsigned short k;
    }

    double k;

    int main()
    {
    long k;

    k = 4000000000L;

    ::k = 87.6575;

    Chocolate::k = 65500;
    }

    -JKop
    JKop, Jul 29, 2004
    #6
  7. David Blasdell

    Howard Guest

    "David Blasdell" <> wrote in message
    news:ceakca$785$...
    > I have noticed (especially in multi-thread examples) :: being but before
    > function names, however I'm unsure what difference this makes.
    >
    > Examples:
    > ::Sleep(0);
    > hThread = ::CreateThread(NULL, 0, ThreadFunc, (LPVOID)kk, NULL, &dwID);
    >
    > What difference does it make, if any?
    >
    >
    > Thanks
    >
    >


    In addition to what John said, there is another place where that is often
    used. That is when you are writing code inside a class, and one of the
    function names in the class tree is the same as a function in the global
    namespace. If you don't use the ::func() form, then you'll end up calling
    the member function instead of the global function.

    I see this a lot in my work. Folks writing the SDKs I use apparently like
    to use the same names for functions they provide in their base classes as
    the functions in the global namespace (things like MoveTo(x,y), for
    example). I suppose they think it's easier to remember that way. But if
    you want to call the global MoveTo instead of your object's MoveTo, then you
    have to remember to prepend with ::. It's an endless headache to me!

    I've known some programmers who always prepend with :: unless they're
    explicitly stating the namespace, or calling a member function. That way,
    if you see MoveTo(x,y), you *know* it's the member function, and can go to
    the class declaration for details. But for that to work, you have to
    *always* follow that rule, or else it just gets confusing!

    -Howard
    Howard, Jul 29, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Honne Gowda A
    Replies:
    2
    Views:
    860
    Karl Heinz Buchegger
    Oct 31, 2003
  2. andy6
    Replies:
    2
    Views:
    752
    andy6 via DotNetMonster.com
    Jun 9, 2006
  3. Replies:
    2
    Views:
    895
    Bengt Richter
    Aug 1, 2005
  4. Richard Tobin
    Replies:
    24
    Views:
    772
  5. Bob
    Replies:
    5
    Views:
    253
Loading...

Share This Page