<legend id='dV5dU'><style id='dV5dU'><dir id='dV5dU'><q id='dV5dU'></q></dir></style></legend>
        <tfoot id='dV5dU'></tfoot>

        <i id='dV5dU'><tr id='dV5dU'><dt id='dV5dU'><q id='dV5dU'><span id='dV5dU'><b id='dV5dU'><form id='dV5dU'><ins id='dV5dU'></ins><ul id='dV5dU'></ul><sub id='dV5dU'></sub></form><legend id='dV5dU'></legend><bdo id='dV5dU'><pre id='dV5dU'><center id='dV5dU'></center></pre></bdo></b><th id='dV5dU'></th></span></q></dt></tr></i><div id='dV5dU'><tfoot id='dV5dU'></tfoot><dl id='dV5dU'><fieldset id='dV5dU'></fieldset></dl></div>
      1. <small id='dV5dU'></small><noframes id='dV5dU'>

        • <bdo id='dV5dU'></bdo><ul id='dV5dU'></ul>

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

        时间:2023-09-26

            <tbody id='NoY9x'></tbody>

                <small id='NoY9x'></small><noframes id='NoY9x'>

                <legend id='NoY9x'><style id='NoY9x'><dir id='NoY9x'><q id='NoY9x'></q></dir></style></legend>
                <tfoot id='NoY9x'></tfoot>
                <i id='NoY9x'><tr id='NoY9x'><dt id='NoY9x'><q id='NoY9x'><span id='NoY9x'><b id='NoY9x'><form id='NoY9x'><ins id='NoY9x'></ins><ul id='NoY9x'></ul><sub id='NoY9x'></sub></form><legend id='NoY9x'></legend><bdo id='NoY9x'><pre id='NoY9x'><center id='NoY9x'></center></pre></bdo></b><th id='NoY9x'></th></span></q></dt></tr></i><div id='NoY9x'><tfoot id='NoY9x'></tfoot><dl id='NoY9x'><fieldset id='NoY9x'></fieldset></dl></div>
                  <bdo id='NoY9x'></bdo><ul id='NoY9x'></ul>
                  本文介绍了构造函数中冒号后的成员变量列表有什么用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在阅读这个 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.

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

                  上一篇:在 C++ 中,只有默认参数的构造函数是默认构造函数吗? 下一篇:Visual Studio C++ 2015 std::codecvt with char16_t 或 char32_t

                  相关文章

                • <legend id='fuiAK'><style id='fuiAK'><dir id='fuiAK'><q id='fuiAK'></q></dir></style></legend>

                  <small id='fuiAK'></small><noframes id='fuiAK'>

                    <tfoot id='fuiAK'></tfoot>

                        <bdo id='fuiAK'></bdo><ul id='fuiAK'></ul>
                    1. <i id='fuiAK'><tr id='fuiAK'><dt id='fuiAK'><q id='fuiAK'><span id='fuiAK'><b id='fuiAK'><form id='fuiAK'><ins id='fuiAK'></ins><ul id='fuiAK'></ul><sub id='fuiAK'></sub></form><legend id='fuiAK'></legend><bdo id='fuiAK'><pre id='fuiAK'><center id='fuiAK'></center></pre></bdo></b><th id='fuiAK'></th></span></q></dt></tr></i><div id='fuiAK'><tfoot id='fuiAK'></tfoot><dl id='fuiAK'><fieldset id='fuiAK'></fieldset></dl></div>