前面加上“::”是什么意思?在 C++ 中调用函数做什么?
可能的重复:
前置双冒号“的含义是什么: :” 到类名?
我一直在查看遗留的 C++ 代码,它有这样的内容:
::putenv(local_tz_char);
::tzset();
在函数调用前面添加“::”的语法意味着什么? Google-fu 让我失望了。
Possible Duplicate:
What is the meaning of prepended double colon “::” to class name?
I have been looking at a legacy C++ code and it had something like this:
::putenv(local_tz_char);
::tzset();
What does this syntax of prepending "::" to the function calls mean? Google-fu is failing me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这意味着编译器将在全局命名空间中查找函数
putenv()
和tzset()
。示例
It means that the functions
putenv()
andtzset()
will be looked up by the compiler in the global namespace.Example
也称为范围解析运算符
维基百科的例子:
Also known as Scope resolution operator
Example from Wikipedia:
昨天(一年多)就类似的问题进行了讨论。也许您可以在这里找到更深入的答案。
前置双冒号的含义是什么: :“?
There was a discussion yesterday (+ a year) on a similar question. Perhaps you can find a more indepth answer here.
What is the meaning of prepended double colon "::"?
意思是:在全局命名空间中查找该函数。
It means: look up the function in the global namespace.
它将使用特定的非限定名称(而不是使用
using
关键字导入的任何名称)。It will use the specifically unqualified name (as opposed to anything imported with the
using
keyword).::
是作用域解析运算符,它告诉编译器在什么作用域中查找该函数。例如,如果您有一个带有局部变量
var
的函数,并且有一个同名的全局变量,则可以选择通过在前面添加作用域解析运算符来访问全局变量:IBM C++ 编译器文档像这样说(来源):
对于类内部的方法和外部同名的版本也可以执行相同的操作。如果您想访问特定命名空间中的变量、函数或类,您可以像这样访问它:::
但有一点需要注意,即使尽管它是一个运算符,但它不是可以重载的运算符之一。
the
::
is the scope resolution operator, it tells the compiler in what scope to find the function.For instance if you have a function with a local variable
var
and you have a global variable of the same name, you can choose to access the global one by prepending the scope resolution operator:The IBM C++ compiler documentation puts it like this (source):
The same can be done for methods inside a class and versions of the same name outside. If you wanted to access a variable, function or class in a specific namespace you could access it like this:
<namespace>::<variable|function|class>
One thing to note though, even though it is an operator it is not one of the operators that can be overloaded.