构造函数中冒号后的成员变量列表有什么用?

What is the member variables list after the colon in a constructor good for?(构造函数中冒号后的成员变量列表有什么用?)

本文介绍了构造函数中冒号后的成员变量列表有什么用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读这个 C++ 开源代码,我来到了一个构造函数,但我不明白(主要是因为我不知道 C++ :P)

I'm reading this C++ open source code and I came to a constructor but I don't get it ( basically because I don't know C++ :P )

我非常了解 C 和 Java.

I understand C and Java very well.

 TransparentObject::TransparentObject( int w, int x, int y, int z ) : 
     _someMethod( 0 ),
     _someOtherMethod( 0 ),
     _someOtherOtherMethod( 0 ),
     _someMethodX( 0 ) 
  {
       int bla;
       int bla;
  }

据我所知,第一行只声明了构造函数名称,::"对我来说听起来像是属于".而 {} 之间的代码是它自身的构造函数体.

As far I can "deduce" The first line only declares the construtor name, the "::" sounds like "belongs to" to me. And the code between {} is the constructor body it self.

我认为"参数后面的内容和第一个{"就像方法默认参数或其他东西,但我在网上找不到合理的解释.我在示例中发现的大多数 C++ 构造函数几乎与 Java 中的相同.

I "think" what's after the paremeters and the first "{" are like methods default parameters or something, but I don't find a reasonable explanation on the web. Most of the C++ constructors that I found in the examples are almost identical to those in Java.

我的假设是对的吗?::"就像属于,params和body后面的列表就像默认参数"什么的?

I'm I right in my assumptions? "::" is like belongs to, and the list after params and body are like "default args" or something?

更新:感谢您的回答.那些可以称为方法吗?(我猜没有),在构造函数体中调用它们有什么区别

UPDATE: Thanks for the answers. May those be called methods? ( I guess no ) and what is the difference of call them within the constructor body

推荐答案

最常见的情况是这样的:

The most common case is this:

class foo{
private:
    int x;
    int y;
public:
    foo(int _x, int _y) : x(_x), y(_y) {}
}

这会将 xy 设置为构造函数中 _x_y 中给出的值参数.这通常是构造任何声明为数据成员的对象的最佳方式.

This will set x and y to the values that are given in _x and _y in the constructor parameters. This is often the best way to construct any objects that are declared as data members.

也有可能您正在查看构造函数链接:

It is also possible that you were looking at constructor chaining:

class foo : public bar{
    foo(int x, int y) : bar(x, y) {}
};

在这种情况下,类的构造函数将调用其基类的构造函数并传递值xy.

In this instance, the class's constructor will call the constructor of its base class and pass the values x and y.

进一步剖析函数:

TransparentObject::TransparentObject( int w, int x, int y, int z ) : 
   _someMethod( 0 ),
   _someOtherMethod( 0 ),
   _someOtherOtherMethod( 0 ),
   _someMethodX( 0 ) 
{
     int bla;
     int bla;
}

:: 运算符称为作用域解析运算符.它基本上只是表明TransparentObjectTransparentObject 的成员.其次,您假设构造函数的主体出现在花括号中是正确的.

The ::-operator is called the scope resolution operator. It basically just indicates that TransparentObject is a member of TransparentObject. Secondly, you are correct in assuming that the body of the constructor occurs in the curly braces.

更新:感谢您的回答.那些可以称为方法吗?(我猜没有),在构造函数体中调用它们有什么区别

UPDATE: Thanks for the answers. May those be called methods? ( I guess no ) and what is the difference of call them within the constructor body

关于这个主题的信息比我给你的要多得多此处.必须使用初始化列表的最常见区域是在初始化引用或 const 时,因为这些变量必须在创建后立即赋值.

There is much more information on this subject than I could possibly ever give you here. The most common area where you have to use initializer lists is when you're initializing a reference or a const as these variables must be given a value immediately upon creation.

这篇关于构造函数中冒号后的成员变量列表有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:构造函数中冒号后的成员变量列表有什么用?

基础教程推荐