• <tfoot id='7aqoE'></tfoot>
    <legend id='7aqoE'><style id='7aqoE'><dir id='7aqoE'><q id='7aqoE'></q></dir></style></legend>

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

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

        const 引用可以分配一个 int 吗?

        时间:2023-12-03
        <i id='qUp1p'><tr id='qUp1p'><dt id='qUp1p'><q id='qUp1p'><span id='qUp1p'><b id='qUp1p'><form id='qUp1p'><ins id='qUp1p'></ins><ul id='qUp1p'></ul><sub id='qUp1p'></sub></form><legend id='qUp1p'></legend><bdo id='qUp1p'><pre id='qUp1p'><center id='qUp1p'></center></pre></bdo></b><th id='qUp1p'></th></span></q></dt></tr></i><div id='qUp1p'><tfoot id='qUp1p'></tfoot><dl id='qUp1p'><fieldset id='qUp1p'></fieldset></dl></div>
            <legend id='qUp1p'><style id='qUp1p'><dir id='qUp1p'><q id='qUp1p'></q></dir></style></legend>

              <tbody id='qUp1p'></tbody>
          1. <small id='qUp1p'></small><noframes id='qUp1p'>

          2. <tfoot id='qUp1p'></tfoot>
              <bdo id='qUp1p'></bdo><ul id='qUp1p'></ul>
                  本文介绍了const 引用可以分配一个 int 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我遇到了一个代码片段

                  const int& reference_to_const_int = 20;
                  cout<<"
                    reference_to_const_int = "<<reference_to_const_int<<endl;     
                  

                  此代码编译 &执行输出:-

                  This code compiles & executes with output :-

                  reference_to_const_int = 20
                  

                  这对我来说很奇怪.据我所知参考不占用内存 &它们是其他变量的别名.因此我们不能说

                  This is something strange in for me. As I know reference do not occupy memory & they are aliases to other variables. hence we cannot say

                  int& reference_to_int = 30;
                  

                  以上语句不能编译给出错误:-

                  The above statement shall not compile giving error :-

                   error: invalid initialization of non-const reference of type ‘int&’ from an rvalue of type ‘int’
                  

                  const int&"究竟发生了什么案件?需要完整的解释.

                  What exactly is happening in the "const int&" case? A full explanation is desired.

                  请帮忙.

                  谢谢

                  推荐答案

                  创建了一个临时对象,将 const 引用绑定到它是合法的,但将它绑定到非 const 引用是非法的code>const 一.

                  A temporary is created, and it's legal to bind a const reference to it, but illegal to bind it to a non-const one.

                  就像:

                  const int& reference_to_const_int = int(20);  //LEGAL
                        int& reference_to_const_int = int(20);  //ILLEGAL
                  

                  const 引用延长了临时文件的生命周期,这就是它起作用的原因.这只是语言的规则.

                  A const reference extends the life of a temporary, that's why this works. It's just a rule of the language.

                  这篇关于const 引用可以分配一个 int 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:为什么我可以为引用分配一个新值,以及如何使引用引用其他内容? 下一篇:链接器错误 C++“未定义引用"

                  相关文章

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

                    1. <small id='k1omE'></small><noframes id='k1omE'>

                      <tfoot id='k1omE'></tfoot>

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