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

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

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

      2. <tfoot id='PQWDj'></tfoot>

      3. 将函数的返回值存储在参考 C++ 中

        Store return value of function in reference C++(将函数的返回值存储在参考 C++ 中)
          <bdo id='xh1hv'></bdo><ul id='xh1hv'></ul>

          1. <legend id='xh1hv'><style id='xh1hv'><dir id='xh1hv'><q id='xh1hv'></q></dir></style></legend>

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

                • 本文介绍了将函数的返回值存储在参考 C++ 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  将对象的返回值存储在引用中是否有效?

                  Is it valid to store the return value of an object in a reference?

                  class A { ... };
                  A myFunction()
                  {
                      A myObject;
                      return myObject;
                  } //myObject goes out of scope here
                  
                  void mySecondFunction()
                  {
                      A& mySecondObject = myFunction();
                  }
                  

                  是否可以这样做以避免将 myObject 复制到 mySecondObject?不再需要 myObject 并且应该与 mySecondObject 完全相同,因此理论上将对象的所有权从一个对象传递给另一个对象会更快.(这也可以使用 boost 共享指针,但它具有共享指针的开销.)

                  Is it possible to do this in order to avoid copying myObject to mySecondObject? myObject is not needed anymore and should be exactly the same as mySecondObject so it would in theory be faster just to pass ownership of the object from one object to another. (This is also possible using boost shared pointer but that has the overhead of the shared pointer.)

                  提前致谢.

                  推荐答案

                  不允许将临时引用绑定到非常量引用,但是如果您使引用为常量,则会将临时引用的生命周期延长到引用,见 这篇 Danny Kalev 关于它的帖子.

                  It is not allowed to bind the temporary to a non-const reference, but if you make your reference const you will extend the lifetime of the temporary to the reference, see this Danny Kalev post about it.

                  简而言之:

                  const A& mySecondObject = myFunction();
                  

                  这篇关于将函数的返回值存储在参考 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++ 引用发生变化)

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

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

                          <tbody id='xYSaE'></tbody>
                      1. <legend id='xYSaE'><style id='xYSaE'><dir id='xYSaE'><q id='xYSaE'></q></dir></style></legend>
                        • <bdo id='xYSaE'></bdo><ul id='xYSaE'></ul>