1. <legend id='5kWMP'><style id='5kWMP'><dir id='5kWMP'><q id='5kWMP'></q></dir></style></legend>

      <small id='5kWMP'></small><noframes id='5kWMP'>

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

        C++ POD 类型何时进行零初始化?

        When do C++ POD types get zero-initialized?(C++ POD 类型何时进行零初始化?)
          <tfoot id='hq5tA'></tfoot><legend id='hq5tA'><style id='hq5tA'><dir id='hq5tA'><q id='hq5tA'></q></dir></style></legend>

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

                  <tbody id='hq5tA'></tbody>

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

                  本文介绍了C++ POD 类型何时进行零初始化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  来自 C 背景,我一直认为 POD 类型(例如 int)在 C++ 中从未自动零初始化,但似乎这是完全错误的!

                  Coming from a C background, I've always assumed the POD types (eg ints) were never automatically zero-initialized in C++, but it seems this was plain wrong!

                  我的理解是,只有裸"的非静态 POD 值不会填充零,如代码片段所示.我做对了吗,还有其他重要的案例我遗漏了吗?

                  My understanding is that only 'naked' non-static POD values don't get zero-filled, as shown in the code snippet. Have I got it right, and are there any other important cases that I've missed?

                  static int a;
                  
                  struct Foo { int a;};
                  
                  void test()
                  {
                    int b;     
                    Foo f;
                    int *c = new(int); 
                    std::vector<int> d(1);
                  
                    // At this point...
                    // a is zero
                    // f.a is zero
                    // *c is zero
                    // d[0] is zero
                    // ... BUT ... b is undefined     
                  }  
                  

                  推荐答案

                  假设你在调用test()之前没有修改aa 的值为零,因为具有静态存储持续时间的对象在程序启动时被零初始化.

                  Assuming you haven't modified a before calling test(), a has a value of zero, because objects with static storage duration are zero-initialized when the program starts.

                  d[0] 的值为零,因为由 std::vector 调用的构造函数;d(1) 有一个采用默认参数的第二个参数;第二个参数被复制到正在构造的向量的所有元素中.默认参数是 T(),所以你的代码等价于:

                  d[0] has a value of zero, because the constructor invoked by std::vector<int> d(1) has a second parameter that takes a default argument; that second argument is copied into all of the elements of the vector being constructed. The default argument is T(), so your code is equivalent to:

                  std::vector<int> d(1, int());
                  

                  b 具有不确定的值是正确的.

                  You are correct that b has an indeterminate value.

                  f.a*c 也有不确定的值.要对它们进行值初始化(对于 POD 类型与零初始化相同),您可以使用:

                  f.a and *c both have indeterminate values as well. To value initialize them (which for POD types is the same as zero initialization), you can use:

                  Foo f = Foo();      // You could also use Foo f((Foo()))
                  int* c = new int(); // Note the parentheses
                  

                  这篇关于C++ POD 类型何时进行零初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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(使用非平凡的构造函数初始化联合)
                  <legend id='dgMUO'><style id='dgMUO'><dir id='dgMUO'><q id='dgMUO'></q></dir></style></legend>
                • <i id='dgMUO'><tr id='dgMUO'><dt id='dgMUO'><q id='dgMUO'><span id='dgMUO'><b id='dgMUO'><form id='dgMUO'><ins id='dgMUO'></ins><ul id='dgMUO'></ul><sub id='dgMUO'></sub></form><legend id='dgMUO'></legend><bdo id='dgMUO'><pre id='dgMUO'><center id='dgMUO'></center></pre></bdo></b><th id='dgMUO'></th></span></q></dt></tr></i><div id='dgMUO'><tfoot id='dgMUO'></tfoot><dl id='dgMUO'><fieldset id='dgMUO'></fieldset></dl></div>
                  <tfoot id='dgMUO'></tfoot>
                      <tbody id='dgMUO'></tbody>
                      <bdo id='dgMUO'></bdo><ul id='dgMUO'></ul>

                          • <small id='dgMUO'></small><noframes id='dgMUO'>