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

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

      1. <small id='7x7XB'></small><noframes id='7x7XB'>

        <tfoot id='7x7XB'></tfoot>

        const 引用的字面初始化

        时间:2023-05-24
              <tbody id='QqCOg'></tbody>
            <legend id='QqCOg'><style id='QqCOg'><dir id='QqCOg'><q id='QqCOg'></q></dir></style></legend>

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

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

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

                2. 本文介绍了const 引用的字面初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  以下代码如何在 C++ 中工作?合乎逻辑吗?

                  How does the following code work in C++? Is it logical?

                  const int &ref = 9;
                  const int &another_ref = ref + 6;
                  

                  为什么 C++ 允许常量引用的字面初始化,而非常量引用不允许?例如:

                  Why does C++ allow literal initialization for const references when the same is not permitted for non-const references? E.g.:

                  const int days_of_week = 7;
                  int &dof = days_of_week; //error: non const reference to a const object
                  

                  这可以通过一个事实来解释,非常量引用可用于更改它所引用的变量的值.因此,C++ 不允许对 const 变量的非常量引用.

                  This can be explained by the fact that, a non-const reference can be used to change the value of the variable it is referring to. Hence, C++ does not permit a non-const reference to a const variable.

                  这是一个可能的解释吗?C++ 不允许:

                  Could this be a possible explanation? C++ does not allow:

                  int &ref = 7;
                  

                  因为这不合逻辑,但是:

                  Because that is not logical, but:

                  const int &ref = 7;
                  

                  几乎相当于:

                  const int val = 7;
                  

                  所以常量变量允许字面初始化.

                  So literal initialization is permitted for const variables.

                  P.S.:我目前正在学习 Lippman 的 C++ Primer.

                  P.S.: I'm currently studying Lippman's C++ Primer.

                  推荐答案

                  所以你可以这样写代码:

                  So you can write code like this:

                  void f( const string & s ) {
                  }
                  
                  f( "foobar" );
                  

                  尽管严格来说,这里实际发生的并不是将文字绑定到 const 引用——而是创建了一个临时字符串对象:

                  Although strictly speaking what is actually happening here is not the literal being bound to a const reference - instead a temprary string object is created:

                  string( "foobar" );
                  

                  并且这个无名字符串绑定到引用.

                  and this nameless string is bound to the reference.

                  请注意,像您这样做时创建非参数引用变量实际上很不寻常 - 引用的主要目的是用作函数参数和返回值.

                  Note that it is actually quite unusual to create non-parameter reference variables as you are doing - the main purpose of references is to serve as function parameters and return values.

                  这篇关于const 引用的字面初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:将变量名转换为 C++ 中的字符串 下一篇:如何使用宏在 C++ 中生成随机变量名称?

                  相关文章

                  最新文章

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

                    <legend id='0zj4X'><style id='0zj4X'><dir id='0zj4X'><q id='0zj4X'></q></dir></style></legend>

                    <small id='0zj4X'></small><noframes id='0zj4X'>

                    • <bdo id='0zj4X'></bdo><ul id='0zj4X'></ul>