为什么 :: (scope) 与空左操作数一起使用?
我已经见过几次了,我一直在挠头想知道为什么......
例如:(http://www.codeguru.com/forum/showthread.php?t=377394)
void LeftClick ( )
{
INPUT Input={0};
// left down
Input.type = INPUT_MOUSE;
Input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
::SendInput(1,&Input,sizeof(INPUT));
// left up
::ZeroMemory(&Input,sizeof(INPUT));
Input.type = INPUT_MOUSE;
Input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
::SendInput(1,&Input,sizeof(INPUT));
}
这个例子无需 ::(作用域)运算符即可工作,那么为什么它们还在那里呢?
I've seen this a few times now, and I've been scratching my head wondering why...
As an example: (http://www.codeguru.com/forum/showthread.php?t=377394)
void LeftClick ( )
{
INPUT Input={0};
// left down
Input.type = INPUT_MOUSE;
Input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
::SendInput(1,&Input,sizeof(INPUT));
// left up
::ZeroMemory(&Input,sizeof(INPUT));
Input.type = INPUT_MOUSE;
Input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
::SendInput(1,&Input,sizeof(INPUT));
}
This example works without the :: (scope) operators so why are they even there?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这基本上意味着“获取全局范围的函数,而不是当前可见的函数”。
This basically mean "get the GLOBAL scoped function, instead of the currently visible one".
假设您有以下内容:
如果您想从成员函数
Foo::bar
调用全局函数bar
,您可以使用左侧为空的语法:Lets say you have the following:
If you want to call the global function
bar
from the member functionFoo::bar
you use the syntax with empty left hand side:它强制进行绝对名称解析。
如果没有它,名称解析将相对于类/函数命名空间路径进行搜索。
因此,假设 LeftClick() 位于命名空间层次结构中:
如果您有嵌套名称,它会变得更有趣:
It forces an absolute name resolution.
Without it name resolution is searched for relative to the class(s)/functions namespace path.
So assume LeftClick() is in the namespace hierarchy:
It becomes more interesting if you have a nested name:
这是为了强制在全局范围内查找符号。
It's to force the symbol to be looked up at global scope.
以这种方式使用作用域运算符意味着您引用的是全局作用域。
为了节省我宝贵的时间和击键次数,请查看没有作用域的作用域解析运算符 。
Using the scope operator in this fashion means that you are referring to the global scope.
To save me valuable time and keystrokes, check out scope resolution operator without a scope.
::
用于直接从对象外部访问该对象。The
::
is used to give access to an object directly from outside of the object.