从函数返回对象时调用 C++ 中的复制构造函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/665781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:35:20  来源:igfitidea点击:

Copy Constructor in C++ is called when object is returned from a function?

c++constructorcopy-constructor

提问by Kazoom

I understand copy constructor is called on three instances

我知道在三个实例上调用了复制构造函数

  1. When instantiating one object and initializing it with values from another object.
  2. When passing an object by value.
  1. 实例化一个对象并使用另一个对象的值对其进行初始化时。
  2. 按值传递对象时。

3. When an object is returned from a function by value.

3. 当一个对象按值从函数返回时。

I have question with no.3 if copy constructor is called when an object value is returned, shouldn't it create problems if object is declared locally in the function.

我对 3 号有疑问,如果在返回对象值时调用复制构造函数,如果在函数中本地声明对象,它不应该产生问题。

i mean the copy constructor is a deep copy one and takes reference of an object as parameter

我的意思是复制构造函数是一个深复制构造函数,并将对象的引用作为参数

采纳答案by sharptooth

It's called exactly to avoid problems. A new object serving as result is initialized from the locally-defined object, then the locally defined object is destroyed.

正是为了避免问题而调用它。从本地定义的对象初始化作为结果的新对象,然后销毁本地定义的对象。

In case of deep-copy user-defined constructor it's all the same. First storage is allocated for the object that will serve as result, then the copy constructor is called. It uses the passed reference to access the locally-defined object and copy what's necessary to the new object.

在深拷贝用户定义的构造函数的情况下,它都是一样的。首先为作为结果的对象分配存储空间,然后调用复制构造函数。它使用传递的引用来访问本地定义的对象并将必要的内容复制到新对象中。

回答by unwind

The copy is done before the called function exits, and copies the then-existing local variable into the return value.

复制在被调用函数退出之前完成,并将当时存在的局部变量复制到返回值中。

The called function has access to the memory the return value will occupy, even though that memory is not "in scope" when the copy is being made, it's still available.

被调用的函数可以访问返回值将占用的内存,即使在进行复制时该内存不在“范围内”,它仍然可用。

回答by xtofl

According to an answerto my question, the copy constructor may be calledeven twice: once to copy a local object onto the return 'object', and once to copy the return object onto the variable it was assigned to.

根据对我的问题的回答,复制构造函数甚至可能被调用两次:一次将本地对象复制到返回的“对象”上,一次将返回对象复制到分配给它的变量上。

However, it needn'tbe! The compiler can optimize both copy constructions away.

然而,大可不必!编译器可以优化这两种复制结构。

回答by Lou Franco

No, it calls it before the locals are destroyed. You can test this with an object that logs destruction and copy construction, or by looking at the generated assembly code.

不,它在当地人被摧毁之前调用它。您可以使用记录销毁和复制构造的对象或通过查看生成的汇编代码来测试这一点。

回答by Bilal Qureshi

There are three general cases where the copy constructor is called:

调用复制构造函数的一般情况有以下三种:

  1. When instantiating one object and initializing it with values from another object (of same type).
  2. When passing an object by value.
  3. When an object is returned from a function by value.
  1. 实例化一个对象并使用来自另一个对象(相同类型)的值对其进行初始化时。
  2. 按值传递对象时。
  3. 当一个对象按值从函数返回时。