<tfoot id='bzopG'></tfoot>

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

        <legend id='bzopG'><style id='bzopG'><dir id='bzopG'><q id='bzopG'></q></dir></style></legend>

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

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

      2. 在构造函数 C++ 中初始化引用

        时间:2023-09-26

        <legend id='EYBW9'><style id='EYBW9'><dir id='EYBW9'><q id='EYBW9'></q></dir></style></legend>
      3. <tfoot id='EYBW9'></tfoot>
            <tbody id='EYBW9'></tbody>

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

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

                  <i id='EYBW9'><tr id='EYBW9'><dt id='EYBW9'><q id='EYBW9'><span id='EYBW9'><b id='EYBW9'><form id='EYBW9'><ins id='EYBW9'></ins><ul id='EYBW9'></ul><sub id='EYBW9'></sub></form><legend id='EYBW9'></legend><bdo id='EYBW9'><pre id='EYBW9'><center id='EYBW9'></center></pre></bdo></b><th id='EYBW9'></th></span></q></dt></tr></i><div id='EYBW9'><tfoot id='EYBW9'></tfoot><dl id='EYBW9'><fieldset id='EYBW9'></fieldset></dl></div>
                  本文介绍了在构造函数 C++ 中初始化引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我不认为这是一个重复的问题.有类似的,但它们并没有帮助我解决我的问题.

                  I don't think is a duplicate question. There are similar ones but they're not helping me solve my problem.

                  根据this,以下内容在 C++ 中有效:

                  According to this, the following is valid in C++:

                  class c {
                  public:
                     int& i;
                  };
                  

                  但是,当我这样做时,出现以下错误:

                  However, when I do this, I get the following error:

                  error: uninitialized reference member 'c::i'
                  

                  如何在构造上成功初始化i=0?

                  How can I initialise successfully do i=0on construction?

                  非常感谢.

                  推荐答案

                  没有空引用"这样的东西.您必须在对象初始化时提供参考.把它放在构造函数的基本初始化列表中:

                  There is no such thing as an "empty reference". You have to provide a reference at object initialization. Put it in the constructor's base initializer list:

                  class c
                  {
                  public:
                    c(int & a) : i(a) { }
                    int & i;
                  };
                  

                  另一种选择是 i(*new int),但这会很糟糕.

                  An alternative would be i(*new int), but that'd be terrible.

                  为了回答您的问题,您可能只想将 i 设为成员对象,而不是引用,所以只需说 int i;,并将构造函数编写为 c() : i(0) {}c(int a = 0) : i(a) { }.

                  To maybe answer your question, you probably just want i to be a member object, not a reference, so just say int i;, and write the constructor either as c() : i(0) {} or as c(int a = 0) : i(a) { }.

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

                  上一篇:在 new (c++) 的构造函数调用中不使用括号 下一篇:C++ 构造函数语法

                  相关文章

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

                      <tfoot id='5vVPT'></tfoot>
                    2. <small id='5vVPT'></small><noframes id='5vVPT'>

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