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

        • <bdo id='7dlw0'></bdo><ul id='7dlw0'></ul>

        <small id='7dlw0'></small><noframes id='7dlw0'>

      1. C++ 构造函数:const 引用初始化时的垃圾

        时间:2023-12-03
      2. <legend id='3gh4R'><style id='3gh4R'><dir id='3gh4R'><q id='3gh4R'></q></dir></style></legend>

                <tbody id='3gh4R'></tbody>

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

                <small id='3gh4R'></small><noframes id='3gh4R'>

                <tfoot id='3gh4R'></tfoot>
                  <bdo id='3gh4R'></bdo><ul id='3gh4R'></ul>
                  本文介绍了C++ 构造函数:const 引用初始化时的垃圾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  这段代码有什么问题,为什么我得到错误的答案:

                  what is wrong with this code, why do I get wrong answer:

                  class X
                  {
                  private:
                          const int a;
                          const int& b;
                  public:
                          X(): a(10) , b(20)
                          {
                          //      std::cout << "constructor : a " << a << std::endl;
                          //      std::cout << "constructor : b " << b << std::endl;
                          }
                  
                          void display()
                          {
                              std::cout << "display():a:" << a << std::endl;
                              std::cout << "display():b:" << b << std::endl;
                  
                          }
                  };
                  
                  
                  int
                  main(void)
                  {
                          X x;
                          x.display();
                  return 0;
                  }
                  

                  上面的代码会给我结果

                  display():a:10
                  display():b:1104441332
                  

                  但是如果我删除默认构造函数中注释的 2 行,它会给我正确的结果,即

                  But If I remove the commented 2 lines inside the default constructor it gives me proper result which is

                  constructor : a 10
                  constructor : b 20
                  display():a:10
                  display():b:20
                  

                  请帮忙,谢谢

                  推荐答案

                  您正在将 b 初始化为对 临时 的引用.

                  You are initializing b as a reference to a temporary.

                  20 被创建并且只存在于构造函数的范围内.

                  The value 20 is created and exists only for the scope of the constructor.

                  此后代码的行为非常有趣 - 在我的机器上,我得到的值与您发布的值不同,但基本行为仍然不确定.

                  The behavior of the code after this is very interesting - on my machine, I get different values from the ones you posted, but the fundamental behavior is still nondeterministic.

                  这是因为当引用指向的值超出范围时,它开始引用垃圾内存,从而产生不可预测的行为.

                  This is because when the value to which the reference points falls out of scope, it begins to reference garbage memory instead, giving unpredictable behavior.

                  参见 const 引用是否会延长一个临时的?;答案 https://stackoverflow.com/a/2784304/383402 链接到 C++ 标准的相关部分,特别是以下文字:

                  See Does a const reference prolong the life of a temporary?; the answer https://stackoverflow.com/a/2784304/383402 links to the relevant section of the C++ standard, specifically the below text:

                  A temporary bound to a reference member in a constructor’s ctor-initializer
                  (12.6.2) persists until the constructor exits.
                  

                  这就是为什么你总是在构造函数的打印中得到正确的值,而且很少(但有时可能!)之后.当构造函数退出时,引用悬空,所有赌注都关闭.

                  This is why you always get the right value in the print within the constructor, and rarely (but possibly sometimes!) after. When the constructor exits, the reference dangles and all bets are off.

                  这篇关于C++ 构造函数:const 引用初始化时的垃圾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:将引用转换为 C++ 中的指针表示 下一篇:C++ 引用、对象和指针之间的区别

                  相关文章

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

                        <bdo id='FdaW4'></bdo><ul id='FdaW4'></ul>
                    1. <small id='FdaW4'></small><noframes id='FdaW4'>