🔒 Locked
::FunctionCall
Advertisement
Original Post
I''m currently reading some code that is totally writen in a class structure. It is interfacing with the WinAPI in calls like:
CClass::FunctionCall()
{
// Retrieve the final client size of the window
::GetClientRect( m_hWnd, &rc );
m_nViewX = rc.left;
}
My question is a simple C++ question. Why is there a :: in front of the GetClientRect? Is that a necessary thing? If I remove it it compiles fine, but I am assuming it is just coding style. So I am guessing it is suppoesed to represent that it''s part of an outside class or this class but not defined in this class? Or what?
Thanks for any information,
David
It''s to force name resolution at the global scope. It''s a habit many MFC programmers form because MFC function declarations often have the same names as actual WinAPI functions. With modern C++ compiler lookup techniques it''s often unnecessary. If it helps, you can think of it as a form of commenting.
It makes it have global namespace. So, if you have that function name in another namespace or your class, it resolves it to the correct function.
[edited by - Newfound Ajarn on February 4, 2004 5:16:47 PM]
[edited by - Newfound Ajarn on February 4, 2004 5:16:47 PM]
so bascily in this case its like saying that this function is outside the declared class and the :: makes the function == to anything with the same name?
Thanks,
David
Thanks,
David
quote:No.
Original post by SiliconToad
so bascily in this case its like saying that this function is outside the declared class and the :: makes the function == to anything with the same name?
Namespaces are the central concept here. Structs/classes are namespaces, but C++ also has explicit namespaces. Symbols are resolves from the current scope outwards, including namespaces. What this means is that an identical symbol in an inner scope obscures (hides) the symbol in the outer scope. Here are two examples:
// Example 1:In Example 2, an entity declared in outer::inner can call outer::Function by prefixing the scope resolution operator ( :: ) to the invocation: ::Function. THe outer value variable can not be addressed in Example 1, however, through any means (AFAIK).
int SomeFunction()
{
int value; // outer
for( int c = 0; c < 100; ++c )
{
int value; // inner, hides outer
// do something with inner
}
return value; // outer, raises error because never initialized
}
// Example 2:
namespace Outer {
void Function();
namespace inner {
void Function();
};
};
Hope that helped.
quote:
Original post by Oluseyiquote:Actually, it may only resolve to the outer namespace. Doing it in a nested (inner) namespace would resolve to a symbol in the outer.
Original post by Newfound Ajarn
It makes it have global namespace.
No, in modern C++, it must resolve to a symbol in the global namespace. The only way it would resolve to a symbol in an outer namespace would be if the global namespace had a using directive for the outer namespace. (Section 3.4.3 paragraph 4 in the current standard)
IIRC, there was an ambiguity in pre-standard C++ implementations where ::A meant "look-up A in the global namespace" on some compilers and mean "look-up A in the immediate enclosing namespace" on other compilers. With the advent of Koenig look-up, the scope resolution operator was standardized for the former usage, because the need for resolution in the immediate enclosing namespace decreased.
quote:
Original post by Oluseyi
In Example 2, an entity declared in outer::inner can call outer::Function by prefixing the scope resolution operator ( :: ) to the invocation: ::Function.
No. In order to access ::Outer::Function(), an expression in the scope of Inner needs to use ::Outer::Function() or Outer::Function(). ::Function() should fail to compile on a modern C++ compiler (unless, of course, there is a Function() in the global namespace).
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement