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

  • <legend id='cculw'><style id='cculw'><dir id='cculw'><q id='cculw'></q></dir></style></legend>
        <tfoot id='cculw'></tfoot>
      1. 初始化列表*参数*评估顺序

        时间:2023-09-26
          <bdo id='8tfU4'></bdo><ul id='8tfU4'></ul>

              <tbody id='8tfU4'></tbody>
          1. <legend id='8tfU4'><style id='8tfU4'><dir id='8tfU4'><q id='8tfU4'></q></dir></style></legend>

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

              <small id='8tfU4'></small><noframes id='8tfU4'>

                  <tfoot id='8tfU4'></tfoot>
                  本文介绍了初始化列表*参数*评估顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  因此,C++ 标准要求类成员按照它们在类中声明的顺序进行初始化,而不是按照它们在任何构造函数的初始化列表中提到的顺序进行初始化.但是,这并不意味着对这些初始化的参数进行评估的顺序有任何关系.我正在使用一个经常传递对序列化对象的引用的系统,我想知道是否可以确保以正确的顺序从中读取位,而与这些位写入对象字段的顺序无关.

                  So, the C++ standard requires that class members be initialized in the order in which they are declared in the class, rather than the order that they're mentioned in any constructor's initializer list. However, this doesn't imply anything about the order in which the arguments to those initializations are evaluated. I'm working with a system that frequently passes references to serialization objects around, and wondering if I can ensure that bits are read from it in the right order, independent of the order in which those bits get written into the object's fields.

                  struct Foo {
                      int a;
                      double b;
                      // I want to be able to do this
                      Foo(SerObj &s)
                      : b(s.readDouble()), a(s.readInt())
                      { }
                      // Rather than this
                      Foo (SerObj &s)
                      {
                          b = s.readDouble();
                          a = s.readInt();
                      }
                  };
                  

                  显然,在声明中对 intsdoubles 之类的东西重新排序并不是什么大不了的事,但有时需要动态分配的更大的对象和东西可能会.

                  Obviously, reordering things like ints and doubles in the declaration is not too big a deal, but bigger objects and things requiring dynamic allocation sometimes can be.

                  推荐答案

                  C++ Standard 12.6.2/3:

                  C++ Standard 12.6.2/3:

                  每个base和member初始化后都有一个序列点(1.9).内存初始化器的表达式列表作为相应基类或成员初始化的一部分进行评估.

                  There is a sequence point (1.9) after the initialization of each base and member. The expression-list of a mem-initializer is evaluated as part of the initialization of the corresponding base or member.

                  初始化的顺序是您在问题中指定的顺序.评估是这个初始化的一部分,初始化不能交错(因为它们之间有一个序列点).

                  The order of the initialization is the one you specified in the question. Evaluation is part of this initialization, and the initializations can't interleave (because there is a sequence point between them).

                  这意味着初始化列表中的函数调用不是按所需顺序调用的,而是按成员声明出现的顺序调用的.

                  That means that the function calls in your initializer lists are not called in the desired order, but in the order in which the member declarations appear.

                  这篇关于初始化列表*参数*评估顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:C++ 构造函数语法 下一篇:C++中的构造函数链

                  相关文章

                  <legend id='3Fya4'><style id='3Fya4'><dir id='3Fya4'><q id='3Fya4'></q></dir></style></legend><tfoot id='3Fya4'></tfoot>

                • <small id='3Fya4'></small><noframes id='3Fya4'>

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