C++::: 是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2282725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ : what is :: for?
提问by sivabudh
If you go to the accepted answer of this post
如果您转到此帖子的已接受答案
Could someone please elaborate on why he uses:
有人可以详细说明他为什么使用:
double temp = ::atof(num.c_str());
and not simply
而不是简单地
double temp = atof(num.c_str());
Also, is it considered a good practice to use that syntax when you use "pure" global functions?
此外,当您使用“纯”全局函数时,使用该语法是否被认为是一种好习惯?
回答by Skilldrick
It says use the global version, not one declared in local scope. So if someone's declared an atof
in your class, this'll be sure to use the global one.
它说使用全局版本,而不是在本地范围内声明的版本。因此,如果有人atof
在您的班级中声明了 an ,则肯定会使用全局的。
Have a look at Wikipedia on this subject:
看看关于这个主题的维基百科:
#include <iostream>
using namespace std;
int n = 12; // A global variable
int main() {
int n = 13; // A local variable
cout << ::n << endl; // Print the global variable: 12
cout << n << endl; // Print the local variable: 13
}
回答by sivabudh
::
is the scope resolution operator. Its use in this scenario, as a unary operator, is to ensure that the name (atof
) is always looked up in the global scope-- this can be useful to prevent name hiding from interfering with the lookup.
::
是范围解析运算符。在这种情况下,它作为一元运算符的用途是确保名称 ( atof
) 始终在全局范围内查找——这对于防止名称隐藏干扰查找非常有用。
回答by Narendra N
The :: operator is scope resolution operator.
:: 运算符是作用域解析运算符。
when used with class specifier as like A::a, it is to access the data memeber a of the class A. when used without any specifier, it access the global variable.
当与类说明符一起使用时,如A::a,它是访问A类的数据成员a。当不带任何说明符使用时,它访问全局变量。
It is used mainly in the following contests.
它主要用于以下比赛。
- To refer to the global variables.
- To refer to the static members of the class
- To avoid ambiguites, when a class inherits from multiple [ non-virtual base ] classes.
- With using directive, to use the base class functions in the derived class, when there is a function in base class with name same as that of the derived class, but for a different type.
- To access the functions defined in the global scope, when you have a function defined with the same signature, as in double temp = ::atof(num.c_str());
- To create objects of the nested classes.
- 引用全局变量。
- 引用类的静态成员
- 为避免歧义,当一个类从多个 [非虚拟基] 类继承时。
- 使用 using 指令,当基类中存在与派生类名称相同但类型不同的函数时,使用派生类中的基类函数。
- 要访问在全局范围内定义的函数,当您有一个使用相同签名定义的函数时,如 double temp = ::atof(num.c_str());
- 创建嵌套类的对象。
回答by Alexandros Gezerlis
::func()
means that this function is not affiliated with any specific class. It is used when there exist many functions with the same name, and to avoid confusion between the one you want and specific member functions you precede the function name with the scope operator.
::func()
意味着这个函数不隶属于任何特定的类。当存在许多具有相同名称的函数时使用它,并且为了避免您想要的函数与特定成员函数之间的混淆,请在函数名称之前使用作用域运算符。
From C++ Primer, 4th edition, section 17.2.1:
来自 C++ Primer,第 4 版,第 17.2.1 节:
"Names defined at global scope - names declared outside any class, function, or namespace - are defined inside the global namespace. The global namespace is implicitly declared and exists in every program. Each file that defines entities at global scope adds those names to the global namespace.
“在全局范围定义的名称——在任何类、函数或命名空间之外声明的名称——在全局命名空间内定义。全局命名空间是隐式声明的并且存在于每个程序中。在全局范围定义实体的每个文件都将这些名称添加到全局命名空间。
The scope operator can be used to refer to members of the global namespace. Because the global namespace is implicit, it does not have a name; the notation
作用域运算符可用于引用全局命名空间的成员。因为全局命名空间是隐式的,所以它没有名字;符号
::member_name
refers to a member of the global namespace."
指的是全局命名空间的成员。”
回答by Ponting
Lets say you have two versions of a function f()
one defined outside a namespace and one defined inside. Now lets say you have one more function g()
in the same namespace. Now if you do f()
inside g()
it will call the one defined in the same namespace. But if you want to call the global version you need to do ::f()
假设您有两个版本的函数,f()
一个在命名空间之外定义,一个在内部定义。现在假设您g()
在同一命名空间中还有一个函数。现在,如果你在f()
里面做,g()
它会调用在同一个命名空间中定义的那个。但是如果你想调用全局版本,你需要做::f()