Do you put :: in front of every Windows API call ?
-
Defenestration wrote:
So, unless you mean to call the member function, you should really put :: in front of all Windows API functions to ensure they get called.
Yes, other wise the compiler won't be able to figure out which function do you want to call.
Defenestration wrote:
Admittedly, it's unlikely you'll create a member function with the same name and signature as a Windows API function, but it's possible.
Yes. But then you can certainly change the member function name to something else by prefixing or suffixing it.
You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_
The problem with not putting the :: in front of Win API's when calling them is that you could introduce a subtle bug; the code would compile OK with you thinking your code was calling the Win API, but instead would actually be calling the member function by mistake.
-
The problem with not putting the :: in front of Win API's when calling them is that you could introduce a subtle bug; the code would compile OK with you thinking your code was calling the Win API, but instead would actually be calling the member function by mistake.
Defenestration wrote:
with you thinking your code was calling the Win API
no, a developer should take care which version should be called, the global one or the other one in the class and should use :: accordingly
You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_
-
Defenestration wrote:
with you thinking your code was calling the Win API
no, a developer should take care which version should be called, the global one or the other one in the class and should use :: accordingly
You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_
Thanks for helping me to think it through. In summary then: :: only needs to be placed in front of Win API functions when they are called from within class member functions, otherwise it's not necessary (ie. when calling a Win API function outside of a class).
modified on Tuesday, December 9, 2008 2:41 AM
-
Thanks for helping me to think it through. In summary then: :: only needs to be placed in front of Win API functions when they are called from within class member functions, otherwise it's not necessary (ie. when calling a Win API function outside of a class).
modified on Tuesday, December 9, 2008 2:41 AM
If you need to call the function declared in global namespace, then use the scope resolution operator, suffixing the API. If you don't do this, the local version of the API will be called. For example, when you write an MFC app, something like
MessageBox
is provided to you by both - the MFC application framework and is as well available in the global namespace. A call toMessageBox
will end up calling the one provided by the application framework, whereas**::**MessageBox
will call the one in the global namespace. There's nothing special to it, just used to specify the scope of the API being called.It is a crappy thing, but it's life -^ Carlo Pallini
-
When programming in C++, do you always put :: in front of every Windows API call to indicate it's in the global namespace, or you you think this makes the code ugly ?
Defenestration wrote:
When programming in C++, do you always put :: in front of every Windows API call
Yes.
"It's supposed to be hard, otherwise anybody could do it!" - selfquote
"High speed never compensates for wrong direction!" - unknown -
When programming in C++, do you always put :: in front of every Windows API call to indicate it's in the global namespace, or you you think this makes the code ugly ?
Yes, but only on mondays
-
Defenestration wrote:
When programming in C++, do you always put :: in front of every Windows API call
Yes.
"It's supposed to be hard, otherwise anybody could do it!" - selfquote
"High speed never compensates for wrong direction!" - unknownHow about C and C++ standard library function calls ?
-
How about C and C++ standard library function calls ?
The scope resolution operator is C++ specific, thus you cannot use it in C.
"It's supposed to be hard, otherwise anybody could do it!" - selfquote
"High speed never compensates for wrong direction!" - unknown -
The scope resolution operator is C++ specific, thus you cannot use it in C.
"It's supposed to be hard, otherwise anybody could do it!" - selfquote
"High speed never compensates for wrong direction!" - unknownBut you can call C library functions from C++ programs. So in this case, would you prefix C library function calls with :: ?
-
But you can call C library functions from C++ programs. So in this case, would you prefix C library function calls with :: ?
Defenestration wrote:
So in this case, would you prefix C library function calls with :: ?
My point is you can't! Try prefixing a call to e.g. strlen() with the scope resolution operator. You'll get a compiler error.
"It's supposed to be hard, otherwise anybody could do it!" - selfquote
"High speed never compensates for wrong direction!" - unknown -
Defenestration wrote:
So in this case, would you prefix C library function calls with :: ?
My point is you can't! Try prefixing a call to e.g. strlen() with the scope resolution operator. You'll get a compiler error.
"It's supposed to be hard, otherwise anybody could do it!" - selfquote
"High speed never compensates for wrong direction!" - unknownRoger Stoltz wrote:
My point is you can't! Try prefixing a call to e.g. strlen() with the scope resolution operator. You'll get a compiler error.
Not if the source filename extension is .cpp
modified on Tuesday, December 9, 2008 5:18 AM