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

    1. <small id='yp8Pa'></small><noframes id='yp8Pa'>

      <legend id='yp8Pa'><style id='yp8Pa'><dir id='yp8Pa'><q id='yp8Pa'></q></dir></style></legend>
      • <bdo id='yp8Pa'></bdo><ul id='yp8Pa'></ul>

        <tfoot id='yp8Pa'></tfoot>

        构造函数初始化 Vs 赋值

        Constructor initialization Vs assignment(构造函数初始化 Vs 赋值)

                <tbody id='rYXwG'></tbody>

                <bdo id='rYXwG'></bdo><ul id='rYXwG'></ul>
                <legend id='rYXwG'><style id='rYXwG'><dir id='rYXwG'><q id='rYXwG'></q></dir></style></legend>
              • <tfoot id='rYXwG'></tfoot>

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

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

                  本文介绍了构造函数初始化 Vs 赋值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  让我们考虑以下类

                  class test1
                  {
                      private:
                          int a;
                          int b;
                      public:
                          test1():a(0),b(0){}
                  };
                  
                  class test2
                  {
                      private:
                          int a;
                          int b;
                      public:
                          test2()
                          {
                              a=0;
                              b=0;
                          }
                  };
                  

                  现在,我知道 test1() 构造函数是初始化 class 的数据成员的正确方法,因为在 test2() 中> 我们正在执行分配而不是初始化.我的问题是:

                  Now, I know that test1() constructor is the right way to initialize the data members of a class, because in test2() we are performing assignment and not initialization. My questions are:

                  1. 如果我们执行赋值而不是初始化会出现什么问题?
                  2. test1() 构造函数的情况下,编译器是否在内部执行赋值?如果没有,那么这些是如何初始化的?
                  1. What might go wrong if we perform assignment instead of initialization?
                  2. Doesn't the compiler internally perform assignment in case of test1() constructor? If not, then how are these initialized?

                  推荐答案

                  如果我们执行赋值而不是初始化会出现什么问题?

                  What might go wrong if we perform assignment instead of initialization?

                  某些类类型(以及引用和const 对象)无法赋值;有些不能被默认初始化;有些默认初始化和重新分配可能比直接初始化更昂贵.

                  Some class types (and also references and const objects) can't be assigned; some can't be default-initialised; some might be more expensive to default-initialise and reassign than to initialise directly.

                  在 test1() 构造函数的情况下,编译器不是在内部执行赋值吗?如果没有,那么这些是如何初始化的?

                  Doesn't the compiler internally performs assignment in case of test1() constructor? If no then how are these initialized?

                  在像 int 这样的原始类型的情况下,两者之间几乎没有或没有实际区别.默认初始化什么都不做,直接初始化和赋值本质上都是一样的.

                  In the case of primitive types like int, there is little or no practical difference between the two. Default-initialisation does nothing, and direct-initialisation and assignment both do essentially the same thing.

                  在类类型的情况下,默认初始化、赋值和直接初始化各自调用不同的用户定义函数,有些操作可能根本不存在;所以总的来说,这两个例子可能有非常不同的行为.

                  In the case of class types, default-initialisation, assignment and direct-initialisation each call different user-defined functions, and some operations may not exist at all; so in general the two examples could have very different behaviour.

                  这篇关于构造函数初始化 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(使用非平凡的构造函数初始化联合)
                  Why is the copy-constructor argument const?(为什么复制构造函数参数是常量?)
                  <i id='wNxn6'><tr id='wNxn6'><dt id='wNxn6'><q id='wNxn6'><span id='wNxn6'><b id='wNxn6'><form id='wNxn6'><ins id='wNxn6'></ins><ul id='wNxn6'></ul><sub id='wNxn6'></sub></form><legend id='wNxn6'></legend><bdo id='wNxn6'><pre id='wNxn6'><center id='wNxn6'></center></pre></bdo></b><th id='wNxn6'></th></span></q></dt></tr></i><div id='wNxn6'><tfoot id='wNxn6'></tfoot><dl id='wNxn6'><fieldset id='wNxn6'></fieldset></dl></div>
                    <legend id='wNxn6'><style id='wNxn6'><dir id='wNxn6'><q id='wNxn6'></q></dir></style></legend>
                        • <bdo id='wNxn6'></bdo><ul id='wNxn6'></ul>

                              <tbody id='wNxn6'></tbody>
                            <tfoot id='wNxn6'></tfoot>

                          1. <small id='wNxn6'></small><noframes id='wNxn6'>