C ++:按引用和复制构造函数返回 [英] C++: returning by reference and copy constructors

查看:175
本文介绍了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 ++:按引用和复制构造函数返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆