:: before function calls

D

David Blasdell

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
 
J

John Harrison

David Blasdell said:
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
 
J

JKop

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
 
H

Howard

David Blasdell said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top