C++中的“::”是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5345527/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:57:59  来源:igfitidea点击:

What does the "::" mean in C++?

c++

提问by Milad Sobhkhiz

What does this symbol mean?

这个符号是什么意思?

AirlineTicket::AirlineTicket()

回答by Erik

:: is the scope resolution operator- used to qualify names. In this case it is used to separate the class AirlineTicketfrom the constructor AirlineTicket(), forming the qualified name AirlineTicket::AirlineTicket()

:: 是范围解析运算符- 用于限定名称。在这种情况下,它用于将类AirlineTicket与构造函数分开AirlineTicket(),形成限定名称AirlineTicket::AirlineTicket()

You use this whenever you need to be explicit with regards to what you're referring to. Some samples:

每当您需要明确您所指的内容时,您都可以使用它。一些示例:

namespace foo {
  class bar;
}
class bar;
using namespace foo;

Now you haveto use the scope resolution operator to refer to a specific bar.

现在您必须使用范围解析运算符来引用特定的柱。

::foo::baris a fully qualified name.

::foo::bar是一个完全限定的名称。

::baris another fully qualified name. (::first means "global namespace")

::bar是另一个完全限定的名称。(::第一个意思是“全局命名空间”)

struct Base {
    void foo();
};
struct Derived : Base {
    void foo();
    void bar() {
       Derived::foo();
       Base::foo();
    }
};

This uses scope resolution to select specific versions of foo.

这使用范围解析来选择特定版本的 foo。

回答by maerics

In C++ the ::is called the Scope Resolution Operator. It makes it clear to which namespace or class a symbol belongs.

在 C++ 中,它::被称为Scope Resolution Operator。它清楚地表明符号属于哪个命名空间或类。

回答by Benedikt Wutzi

It declares a namespace. So in AirlineTicket:: you can call all public functions of the AirlineTicket class and AirlineTicket() is the function in that namespace (in this case the constructor).

它声明了一个命名空间。因此,在 AirlineTicket:: 中,您可以调用 AirlineTicket 类的所有公共函数,而 AirlineTicket() 是该命名空间中的函数(在本例中为构造函数)。

回答by Chris

AirlineTicket is like a namespace for your class. You have to use it in the implementation of the constructor.

AirlineTicket 就像您的类的命名空间。您必须在构造函数的实现中使用它。