<tfoot id='bQYdj'></tfoot>

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

    2. <legend id='bQYdj'><style id='bQYdj'><dir id='bQYdj'><q id='bQYdj'></q></dir></style></legend>

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

      从 C++ 函数返回对象的正确方法是什么?

      What is the proper way to return an object from a C++ function?(从 C++ 函数返回对象的正确方法是什么?)
          <tbody id='cw9rp'></tbody>

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

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

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

                问题描述

                我很困惑:

                • 返回一个对象(但是该对象是从函数中的局部变量复制而来的,这会消耗内存)
                • 返回一个指针(但是你必须记住在调用代码中删除它,这很奇怪)
                • 返回一个引用(但这是不可能的,因为这将是对函数局部变量的引用,一旦函数结束,它就会被删除)
                • returning an object (but then the object is copied from the local variable in the function, which consumes memory)
                • returning a pointer (but then you have to remember to delete it, in the calling code, which is weird)
                • returning a reference (but this is not possible because this would be a reference to a local variable of the function, which would be deleted as soon as the function ends)

                我正在寻找在基本 C++ 中从 C++ 函数返回对象的正确方法(没有库处理指针和自动释放内存).我想知道这应该怎么做.

                I am looking for the proper way to return an object from a C++ function, in basic C++ (no library handling pointers and freeing memory automatically). I want to know how this is supposed to be done.

                谢谢

                推荐答案

                现代编译器通常实现 (命名)返回值优化,通过它您引用的副本(并且在逻辑上期望)没有完成.

                Modern compilers typically implement the (Named) Return Value Optimization, by which the copy you reference (and would logically expect) is not done.

                从 Visual Studio 2005 (VC++ 8.0) 开始,我就不再考虑返回对象.

                Ever since Visual Studio 2005 (VC++ 8.0) I don't think twice about returning objects.

                这篇关于从 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++ 引用发生变化)

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

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

                      <tbody id='DAs45'></tbody>

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