:: :: 在 C++ 中是什么意思?

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

What does : :: mean in C++?

c++syntax

提问by Dawson

Possible Duplicate:
What is this weird colon-member syntax in the constructor?

可能的重复:
构造函数中这个奇怪的冒号成员语法是什么?

I thought i knew everything but something always seems to pop up. Maybe i am forgetting something. What does the : ::namemean? I suspect ::googlemeans from the global namespace use google, protobuf, message. But what does the :do before it? Theres no text on the left so it cant be a label (or can it?!). So what is it?

我以为我什么都知道,但似乎总有一些东西突然出现。也许我忘记了什么。这是什么: ::name意思?我怀疑::google来自全局命名空间的意思是使用google, protobuf, message。但是:在它之前做什么?左边没有文字,所以它不能是标签(或者可以吗?!)。那是什么?

Namespace::Namespace()
  : ::google::protobuf::Message() {
  SharedCtor();
}

-edit- I feel silly, the indention got me. I thought i was looking inside the function body. I was so hoping it would be something new.

-edit- 我觉得很傻,缩进让我知道了。我以为我正在查看函数体内部。我非常希望它会是新的东西。

回答by EboMike

In a constructor, using a colon is used for variable initialization and/or calling the parent constructor. For example:

在构造函数中,使用冒号用于变量初始化和/或调用父构造函数。例如:

struct foo {
   foo();
   foo(int var);
};

struct bar : public foo {
   bar();
   int x;
};

Now you could do bar's constructor like this:

现在你可以像这样做 bar 的构造函数:

bar::bar()
: x(5)
{
}

That sets x to 5. Or:

这将 x 设置为 5。或者:

bar::bar()
: foo(8)
, x(3)
{
}

That uses foo's second constructor with 8 as an argument, then sets x to 3.

它使用 foo 的第二个构造函数和 8 作为参数,然后将 x 设置为 3。

It just looks funny in your code since you have the combination of :for the initialization and ::to get to global namespace.

它在您的代码中看起来很有趣,因为您拥有:初始化和::获取全局命名空间的组合。

回答by Dawson

The first colon :is actually there as an indication that what follows is an initializer list. This can appear in a class's constructor as a way to give that class's data members some initial value (hence the name) before the body of the constructor actually executes.

第一个冒号:实际上是存在的,表示接下来是一个初始化列表。这可以出现在类的构造函数中,作为在构造函数的主体实际执行之前为该类的数据成员提供一些初始值(因此名称)的一种方式。

A small example, formatted differently:

一个小例子,格式不同:

class Foo {
public:
    Foo() :
        x(3),       // int initialized to 3
        str("Oi!"), // std::string initialized to be the string, "Oi!"
        list(10)    // std::vector<float> initialized with 10 values
    { /* constructor body */ }

private:
    int x;
    std::string str;
    std::vector<float> list;
};

EDIT

编辑

As an additional note, if you have a class that subclasses another, the way you invoke your superclass constructor is exactly like this, inside the initializer list. However, instead of specifying the name of a member, you specify the name of the superclass.

作为附加说明,如果您有一个类是另一个类的子类,则在初始化列表中调用超类构造函数的方式与此完全相同。但是,不是指定成员的名称,而是指定超类的名称。

回答by Etienne de Martel

The ::refers to the global scope. For instance:

::指全球范围。例如:

void f() { ... } // (1)

namespace ns
{
    void f() { ... } // (2)

    void g()
    {
        f(); // calls (2)
        ::f(); // calls (1)
    }
}

回答by Garet Claborn

Seems to be working with inheritance, extending from the global namespace.

似乎正在使用继承,从全局命名空间扩展。

class baseClass{ 
    public: 
    int someVal; 
};

class childClass : baseClass
{
    public:
    int AnotherVal;
}

Other answers provided later are much more robust.

稍后提供的其他答案要可靠得多。