• <bdo id='opV4E'></bdo><ul id='opV4E'></ul>

  • <tfoot id='opV4E'></tfoot>
  • <small id='opV4E'></small><noframes id='opV4E'>

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

        在 C++ 中将 char 分配给 int 引用和 const int 引用

        时间:2023-10-18

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

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

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

                1. 本文介绍了在 C++ 中将 char 分配给 int 引用和 const int 引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我注意到将 char 分配给 const int& 可以编译,但将其分配给 int& 会导致编译错误.

                  I noticed that assigning a char to a const int& compiles, but assigning it to a int& gives a compilation error.

                  char c;
                  int& x = c;    // this fails to compile
                  const int& y = c;    // this is ok
                  

                  我知道这样做不是一个好的做法,但我很想知道发生这种情况的原因.

                  I understand that it is not a good practice to do this, but I am curious to know the reason why it happens.

                  我通过寻找分配给不同类型的引用"、将 char 分配给 int 引用"和常量引用和非常量引用之间的差异"来搜索答案,并遇到了许多有用的帖子(int vs const int& , 将字符分配给 int 变量时的奇怪行为 , 在 C 和 C++ 中将 char 转换为 int ,引用和作为函数参数的常量引用之间的区别?),但它们似乎没有解决我的问题.

                  I have searched for an answer by looking for "assigning to reference of different type", "assigning char to a int reference", and "difference between const reference and non-const reference", and came across a number of useful posts (int vs const int& , Weird behaviour when assigning a char to a int variable , Convert char to int in C and C++ , Difference between reference and const reference as function parameter?), but they do not seem to be addressing my question.

                  如果之前已经回答过这个问题,我深表歉意.

                  My apologies if this has been already answered before.

                  推荐答案

                  int& x = c;
                  

                  此处由编译器执行从 charint 的隐式转换.生成的临时 int 只能绑定到 const 引用.绑定到 const int& 还将延长临时结果的生命周期以匹配它所绑定到的引用的生命周期.

                  Here an implicit conversion from char to int is being performed by the compiler. The resulting temporary int can only be bound to a const reference. Binding to a const int& will also extend the lifetime of the temporary result to match that of the reference it is bound to.

                  这篇关于在 C++ 中将 char 分配给 int 引用和 const int 引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:为什么转换为指针然后取消引用? 下一篇:static_cast<T> 是什么?对 T&amp; 做些什么?

                  相关文章

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

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

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