• <legend id='lpfoe'><style id='lpfoe'><dir id='lpfoe'><q id='lpfoe'></q></dir></style></legend>

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

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

        将函数的返回值分配给引用 C++?

        Assign return value from function to a reference c++?(将函数的返回值分配给引用 C++?)
      1. <small id='AXuV7'></small><noframes id='AXuV7'>

        <tfoot id='AXuV7'></tfoot>
            <tbody id='AXuV7'></tbody>
          <legend id='AXuV7'><style id='AXuV7'><dir id='AXuV7'><q id='AXuV7'></q></dir></style></legend>

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

                  问题描述

                  这是一个两部分的问题.可以将函数的返回值分配给引用吗?比如

                  This is a two part question. Is it ok to assign the return value of a function to a reference? Such as

                  Foo FuncBar()
                  {
                      return Foo();
                  }
                  
                  // some where else
                  Foo &myFoo = FuncBar();
                  

                  这样好吗?我的理解是 FuncBar() 返回一个 Foo 对象,现在 myFoo 是对它的引用.

                  Is this ok? Its my understanding that FuncBar() returns a Foo object and now myFoo is a reference to it.

                  问题的第二部分.这是优化吗?因此,如果您经常在循环中执行此操作,最好这样做

                  Second part of the question. Is this an optimization? So if your doing it in a loop a lot of the time is it better to do

                  Foo &myFoo = FuncBar();
                  

                  Foo myFoo = FuncBar();
                  

                  考虑到变量的使用,使用 ref 会不会需要更慢的解引用?

                  And take into account the variables use, won't using the ref require slower dereferences?

                  推荐答案

                  Foo &myFoo = FuncBar();
                  

                  不会编译.应该是:

                  const Foo &myFoo = FuncBar();
                  

                  因为 FuncBar() 返回一个临时对象(即右值)并且只有左值可以绑定到对非常量的引用.

                  because FuncBar() returns a temporary object (i.e., rvalue) and only lvalues can be bound to references to non-const.

                  安全吗?

                  是的,它是安全的.

                  C++ 标准规定,将临时对象绑定到对 const 的引用可将临时对象的生命周期延长至引用本身的生命周期,从而避免常见的悬空引用错误.

                  C++ standard specifies that binding a temporary object to a reference to const lengthens the lifetime of the temporary to the lifetime of the reference itself, and thus avoids what would otherwise be a common dangling-reference error.

                  Foo myFoo = FuncBar();      
                  

                  复制初始化.
                  它创建由 FuncBar() 返回的对象的副本,然后使用该副本来初始化 myFoo.myFoo 是语句执行后一个单独的对象.

                  Is Copy Initialization.
                  It creates a copy of the object returned by FuncBar() and then uses that copy to initalize myFoo. myFoo is an separate object after the statement is executed.

                  const Foo &myFoo = FuncBar();
                  

                  FuncBar() 返回的临时对象绑定到引用 myFoo,注意 myFoo 只是返回的临时对象的别名,而不是一个单独的对象.

                  Binds the temporary returned by FuncBar() to the reference myFoo, note that myFoo is just an alias to the returned temporary and not a separate object.

                  这篇关于将函数的返回值分配给引用 C++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  C++ stl unordered_map implementation, reference validity(C++ stl unordered_map 实现,参考有效性)
                  C++: Is it possible to use a reference as the value in a map?(C++:是否可以使用引用作为映射中的值?)
                  Where ampersand quot;amp;quot; can be put when passing argument by reference?(其中符号“amp;通过引用传递参数时可以放置吗?)
                  Why can a non-const reference parameter be bound to a temporary object?(为什么可以将非常量引用参数绑定到临时对象?)
                  What is a dangling reference?(什么是悬空引用?)
                  C++ reference changes when push_back new element to std::vector(当 push_back 新元素到 std::vector 时,C++ 引用发生变化)
                    <bdo id='UN16o'></bdo><ul id='UN16o'></ul>
                  • <small id='UN16o'></small><noframes id='UN16o'>

                        <tbody id='UN16o'></tbody>

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

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