473,387 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

:: before function calls

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
Jul 22 '05 #1
5 10665

"David Blasdell" <kd@freeworldonline.com> wrote in message
news:ce**********@sparta.btinternet.com...
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
Jul 22 '05 #2
>
void sod()
{
::func();
}
}


Typo should be

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

john
Jul 22 '05 #3
David Blasdell posted:
Thanks, I understand now :)

David

"John Harrison" <jo*************@hotmail.com> wrote in message news:2m************@uni-berlin.de...
>
> 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
Jul 22 '05 #4
> 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
Jul 22 '05 #5

"David Blasdell" <kd@freeworldonline.com> wrote in message
news:ce**********@sparta.btinternet.com...
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
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
by: Bo Peng | last post by:
Dear list, I have many dictionaries with the same set of keys and I would like to write a function to calculate something based on these values. For example, I have a = {'x':1, 'y':2} b =...
3
by: Hugh Macdonald | last post by:
I've got a pure python module that parses a certain type of file. It has a load() function that allows a callback function to be passed for getting progress information. In straight python, this...
3
by: Janross | last post by:
I'm having trouble with a query that's prohibitively slow. On my free-standing office computer it's fine (well, 2-4 seconds), but on the client's network, it takes at least 5 minutes to run. ...
2
by: Matthew Caesar | last post by:
I've written some code in the C# programming language. I have a lot of calls to a function that prints out some debugging information. I would like to occasionally remove all calls to these...
13
by: Bern McCarty | last post by:
I have run an experiment to try to learn some things about floating point performance in managed C++. I am using Visual Studio 2003. I was hoping to get a feel for whether or not it would make...
6
by: Dasn | last post by:
Hi, there. 'lines' is a large list of strings each of which is seperated by '\t' I wanna split each string into a list. For speed, using map() instead of 'for' loop. 'map(str.split, lines)'...
11
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the...
2
by: mosesdinakaran | last post by:
Hi everybody, Today I faced a problem where I am very confused and I could not solve it and I am posting here.... My question is Is is possible to return a value to a particular function ...
6
by: RandomElle | last post by:
Hi there I'm hoping someone can help me out with the use of the Eval function. I am using Access2003 under WinXP Pro. I can successfully use the Eval function and get it to call any function with...
6
by: jmDesktop | last post by:
In a function that takes another function (function pointer) as a argument, or the callback function, which is the one that "calls back"? I'm having a hard time understanding the language. Am I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.