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

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

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

        为什么显式允许默认构造函数和具有 2 个或更多(非默认)参数的构造函数?

        Why is explicit allowed for default constructors and constructors with 2 or more (non-default) parameters?(为什么显式允许默认构造函数和具有 2 个或更多(非默认)参数的构造函数?)
        <tfoot id='0NOhB'></tfoot>

          <tbody id='0NOhB'></tbody>
        • <bdo id='0NOhB'></bdo><ul id='0NOhB'></ul>

          • <i id='0NOhB'><tr id='0NOhB'><dt id='0NOhB'><q id='0NOhB'><span id='0NOhB'><b id='0NOhB'><form id='0NOhB'><ins id='0NOhB'></ins><ul id='0NOhB'></ul><sub id='0NOhB'></sub></form><legend id='0NOhB'></legend><bdo id='0NOhB'><pre id='0NOhB'><center id='0NOhB'></center></pre></bdo></b><th id='0NOhB'></th></span></q></dt></tr></i><div id='0NOhB'><tfoot id='0NOhB'></tfoot><dl id='0NOhB'><fieldset id='0NOhB'></fieldset></dl></div>
            <legend id='0NOhB'><style id='0NOhB'><dir id='0NOhB'><q id='0NOhB'></q></dir></style></legend>
          • <small id='0NOhB'></small><noframes id='0NOhB'>

                1. 本文介绍了为什么显式允许默认构造函数和具有 2 个或更多(非默认)参数的构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我知道带有一个(非默认)参数的构造函数就像隐式转换器一样,从参数类型转换为类类型.但是,explicit 可用于限定任何构造函数,无参数(默认构造函数)或具有 2 个或更多(非默认)参数的构造函数.

                  I understand that constructors with one (non-default) parameter act like implicit convertors, which convert from that parameter type to the class type. However, explicit can be used to qualify any constructor, those with no parameters (default constructor) or those with 2 or more (non-default) parameters.

                  为什么在这些构造函数上允许显式?是否有任何示例说明这有助于防止某种隐式转换?

                  Why is explicit allowed on these constructors? Is there any example where this is useful to prevent implicit conversion of some sort?

                  推荐答案

                  一个原因当然是因为它没有伤害.

                  One reason certainly is because it doesn't hurt.

                  需要它的一个原因是,如果您有第一个参数的默认参数.构造函数变为默认构造函数,但仍可用作转换构造函数

                  One reason where it's needed is, if you have default arguments for the first parameter. The constructor becomes a default constructor, but can still be used as converting constructor

                  struct A {
                    explicit A(int = 0); // added it to a default constructor
                  };
                  

                  C++0x 将它实际用于多参数构造函数.在 C++0x 中,初始化列表可用于初始化类对象.哲学是

                  C++0x makes actual use of it for multi parameter constructors. In C++0x, an initializer list can be used to initialize a class object. The philosophy is

                  • 如果您使用 = { ... },那么您将使用某种复合值"初始化对象,该复合值"在概念上表示对象的抽象值,并且您想要已转换为类型.

                  • if you use = { ... }, then you initialize the object with a sort of "compound value" that conceptually represents the abstract value of the object, and that you want to have converted to the type.

                  如果使用 { ... } 初始化程序,则直接调用对象的构造函数,不一定要指定转换.

                  if you use a { ... } initializer, you directly call the constructors of the object, not necessarily wanting to specify a conversion.

                  考虑这个例子

                  struct String {
                      // this is a non-converting constructor
                      explicit String(int initialLength, int capacity);
                  };
                  
                  struct Address {
                      // converting constructor
                      Address(string name, string street, string city);
                  };
                  
                  String s = { 10, 15 }; // error!
                  String s1{10, 15}; // fine
                  
                  Address a = { "litb", "nerdsway", "frankfurt" }; // fine
                  

                  通过这种方式,C++0x 表明 C++03 的决定,允许在其他构造函数上显式,根本不是一个坏主意.

                  In this way, C++0x shows that the decision of C++03, to allow explicit on other constructors, wasn't a bad idea at all.

                  这篇关于为什么显式允许默认构造函数和具有 2 个或更多(非默认)参数的构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Constructor initialization Vs assignment(构造函数初始化 Vs 赋值)
                  Is a `=default` move constructor equivalent to a member-wise move constructor?(`=default` 移动构造函数是否等同于成员移动构造函数?)
                  Has the new C++11 member initialization feature at declaration made initialization lists obsolete?(声明时新的 C++11 成员初始化功能是否使初始化列表过时了?)
                  Order of constructor call in virtual inheritance(虚继承中构造函数调用的顺序)
                  How to use sfinae for selecting constructors?(如何使用 sfinae 选择构造函数?)
                  Initializing a union with a non-trivial constructor(使用非平凡的构造函数初始化联合)

                    <tbody id='aUmJn'></tbody>

                2. <tfoot id='aUmJn'></tfoot>
                3. <legend id='aUmJn'><style id='aUmJn'><dir id='aUmJn'><q id='aUmJn'></q></dir></style></legend>

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

                            <bdo id='aUmJn'></bdo><ul id='aUmJn'></ul>

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