• <small id='zkQZI'></small><noframes id='zkQZI'>

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

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

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

        <tfoot id='zkQZI'></tfoot>

        C++:通过引用返回和复制构造函数

        C++: returning by reference and copy constructors(C++:通过引用返回和复制构造函数)
        • <bdo id='d9okw'></bdo><ul id='d9okw'></ul>
          • <legend id='d9okw'><style id='d9okw'><dir id='d9okw'><q id='d9okw'></q></dir></style></legend><tfoot id='d9okw'></tfoot>

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

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

                  本文介绍了C++:通过引用返回和复制构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  C++ 中的引用让我感到困惑.:)

                  References in C++ are baffling me. :)

                  基本思想是我试图从函数返回一个对象.如果可能的话,我想不返回指针(因为那样我必须手动delete它)并且不调用复制构造函数(为了效率,自然添加: 也是因为我想知道我是否无法避免编写复制构造函数).

                  The basic idea is that I'm trying to return an object from a function. I'd like to do it without returning a pointer (because then I'd have to manually delete it), and without calling the copy-constructor, if possible (for efficiency, naturally added: and also because I wonder if I can't avoid writing a copy constructor).

                  总而言之,以下是我发现的执行此操作的选项:

                  So, all in all, here are the options for doing this that I have found:

                  • 函数返回类型可以是类本身 (MyClass fun() { ... }) 或对类的引用 (MyClass& fun() { ...}).
                  • 函数可以在返回行构造变量 (return MyClass(a,b,c);) 或返回现有变量 (MyClass x(a,b,c); return x;).
                  • 接收变量的代码也可以有任何类型的变量:(MyClass x = fun();MyClass& x = fun();)
                  • 接收变量的代码可以动态创建一个新变量 (MyClass x = fun();) 或将其分配给现有变量 (MyClass x; x =fun();)
                  • The function return type can be either the class itself (MyClass fun() { ... }) or a reference to the class (MyClass& fun() { ... }).
                  • The function can either construct the variable at the line of return (return MyClass(a,b,c);) or return an existing variable (MyClass x(a,b,c); return x;).
                  • The code that receives the variable can also have a variable of either type: (MyClass x = fun(); or MyClass& x = fun();)
                  • The code which receives the variable can either create a new variable on the fly (MyClass x = fun();) or assign it to an existing variable (MyClass x; x = fun();)

                  还有一些想法:

                  • 返回类型 MyClass& 似乎是一个坏主意,因为这总是导致变量在返回之前被销毁.
                  • 复制构造函数似乎只在我返回现有变量时参与.当返回在返回行中构造的变量时,它永远不会被调用.
                  • 当我将结果分配给现有变量时,析构函数也总是在返回值之前启动.此外,没有调用复制构造函数,但目标变量确实接收从函数返回的对象的成员值.
                  • It seems to be a bad idea to have the return type MyClass& because that always results in the variable being destroyed before it gets returned.
                  • The copy constructor only seems to get involved when I return an existing variable. When returning a variable constructed in the line of return, it never gets called.
                  • When I assign the result to an existing variable, the destructor also always kicks in before the value is returned. Also, no copy constructor gets called, yet target variable does receive the member values of the object returned from the function.

                  这些结果太不一致了,我感到完全困惑.那么,这里究竟发生了什么?我应该如何正确构造和从函数返回一个对象?

                  These results are so inconsistent that I feel totally confused. So, what EXACTLY is happening here? How should I properly construct and return an object from a function?

                  推荐答案

                  理解 C++ 中的复制的最好方法通常是不要尝试生成一个人工示例并对其进行检测 - 允许编译器删除和添加复制构造函数电话,或多或少,因为它认为合适.

                  The best way to understand copying in C++ is often NOT to try to produce an artificial example and instrument it - the compiler is allowed to both remove and add copy constructor calls, more or less as it sees fit.

                  底线 - 如果您需要返回一个值,请返回一个值,不要担心任何费用".

                  Bottom line - if you need to return a value, return a value and don't worry about any "expense".

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

                        <tbody id='AMaEc'></tbody>
                      <tfoot id='AMaEc'></tfoot>

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

                      • <small id='AMaEc'></small><noframes id='AMaEc'>

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