C++ 类使用双冒号

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

C++ classes use of double colon

c++

提问by user2444217

I am learning C++. Now I don't fully understand what this does

我正在学习 C++。现在我不完全明白这是做什么的

Some_Class::Some_Class {
    etc...
}

I would do some research for myself, but I'm not sure where to begin or what's it called. Help would be appreciated.

我会为自己做一些研究,但我不确定从哪里开始或它叫什么。帮助将不胜感激。

回答by AnT

There's no way to say what it is, since the "code" you posted is invalid and ambiguous.

无法说出它是什么,因为您发布的“代码”无效且模棱两可。

  • It could be a nested class definition made in out-of-class fashion. When you define nested classes, you can immediately define the inner class inside, as in

    class Some_Class {   // <- definition of the outer class
      ...
      class SomeClass {  // <- definition of the inner class
        ...
      };
      ...
    };
    

    Or, if you prefer, you can only declarethe nested class inside, and move the actual definitionoutside

    class Some_Class {   // <- definition of the outer class
      ...
      class SomeClass;   // <- declaration of the inner class
      ...
    }; 
    
    class Some_Class::SomeClass { // <- definition of the inner class
      ...
    };
    

    However, for that it has to begin with class/struct, which is not present in what you posted.

  • Or it could be a definition of member function SomeClassof class Some_Class.

    class Some_Class {
      ...
      void SomeClass(int i);          // <- declaration of member function
      ...
    };
    
    void Some_Class::SomeClass(int i) // <- definition of member function
    {
      ...
    }
    

    But for that it has to include return type and parameter list.

  • Or it could be a definition of a static member with {}-enclosed initializer

    class Some_Class {
      ...
      static int SomeClass;
      ...
    };
    
    int Some_Class::SomeClass { 42 };
    

    But for that it has to include static member's type.

  • 它可能是以类外方式制作的嵌套类定义。当你定义嵌套类时,你可以立即在里面定义内部类,如

    class Some_Class {   // <- definition of the outer class
      ...
      class SomeClass {  // <- definition of the inner class
        ...
      };
      ...
    };
    

    或者,如果您愿意,您只能在内部声明嵌套类,并将实际定义移到外部

    class Some_Class {   // <- definition of the outer class
      ...
      class SomeClass;   // <- declaration of the inner class
      ...
    }; 
    
    class Some_Class::SomeClass { // <- definition of the inner class
      ...
    };
    

    但是,为此它必须以 开头class/struct,这在您发布的内容中不存在。

  • 或者它可以是SomeClassclass成员函数的定义Some_Class

    class Some_Class {
      ...
      void SomeClass(int i);          // <- declaration of member function
      ...
    };
    
    void Some_Class::SomeClass(int i) // <- definition of member function
    {
      ...
    }
    

    但为此它必须包括返回类型和参数列表。

  • 或者它可以是一个带有{}-enclosed 初始值设定项的静态成员的定义

    class Some_Class {
      ...
      static int SomeClass;
      ...
    };
    
    int Some_Class::SomeClass { 42 };
    

    但是为此它必须包含静态成员的类型。

In other words, there's no way to say what it is you posted and what your question is really about.

换句话说,无法说出您发布的内容以及您的问题的真正含义。

回答by Captain Skyhawk

The :: resolves either a class or namespace.

:: 解析类或命名空间。

For example

例如

namespace test1 { int i = 0; }
cout << test1::i << endl;

or

或者

class test2 { 
     public: 
     static int i = 0; 
 };
 // after in
cout << test2::i << endl;

You can also add this:

你也可以添加这个:

using namespace test1;
cout << i << endl;

回答by Shf

You are confused by the scope resolution operator(thanks @Huytard for the link)

您对范围解析运算符感到困惑(感谢 @Huytard 提供链接)

:: is the scope resolution operator - it means, that SomeClassmethod is in Some_Class, given your example -Some_Class::SomeClass

:: 是范围解析运算符 - 这意味着,该SomeClass方法在 中Some_Class,给定你的例子 -Some_Class::SomeClass