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

发布于 2024-07-15 22:45:23 字数 232 浏览 10 评论 0原文

会在三个实例上调用复制构造函数

  1. 我知道在实例化一个对象并使用另一个对象的值对其进行初始化时,
  2. 。 按值传递对象时。

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

我对第 3 点有疑问 如果在返回对象值时调用复制构造函数,并且在函数中本地声明对象,那么它不会产生问题吗?

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

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.

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

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

影子的影子 2024-07-22 22:45:23

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

对于深复制用户定义的构造函数来说,它们都是一样的。 首先为将作为结果的对象分配存储空间,然后调用复制构造函数。 它使用传递的引用来访问本地定义的对象并将所需内容复制到新对象。

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.

抚你发端 2024-07-22 22:45:23

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

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

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.

梦归所梦 2024-07-22 22:45:23

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

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

According to an answer to my question, the copy constructor may be called even 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't be! The compiler can optimize both copy constructions away.

梦晓ヶ微光ヅ倾城 2024-07-22 22:45:23

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

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.

尤怨 2024-07-22 22:45:23

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

  1. 实例化一个对象并使用另一个对象(相同类型)的值对其进行初始化时。
  2. 按值传递对象时。
  3. 当函数按值返回对象时。

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文