可以将对象作为值传递给副本构造函数 [英] Can a object be passed as value to the copy constructor

查看:124
本文介绍了可以将对象作为值传递给副本构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些混淆这个一个对象可以作为值传递给复制构造函数
编译器清楚地拒绝这,这意味着它是不可能的。
你能帮我理解一下吗?

I have some confusion with this "Can a object be passed as value to the copy constructor" The compiler clearly rejects this, which means it is not possible. Can you help me understand this?

class Dummy{
    Dummy(Dummy dummy){ // This is not possible
    }
};

那么为什么说复制构造函数会导致递归调用时使用pass by value。

Then why is it said that "Copy constructor will lead to recursive calls when pass by value is used."

推荐答案

这是因为要传递值,您需要 COPY 因此,你在传递对象的副本,如何复制它的定义。

This is because in order to pass by value, you need to COPY the object. Therefore, you are passing in a copy of the object, in the definition of how to copy it.

有效的真正发生,如果这个陷阱不存在,是你的副本构造函数将调用你的拷贝构造函数创建一个拷贝,它将调用你的拷贝构造函数来创建一个拷贝,它将调用你的拷贝构造函数来创建拷贝,...

Effectively what really happens if this trap is not there, is your copy constructor will call your copy constructor to create a copy, which will call your copy constructor to create a copy, which will call your copy constructor to create a copy, ...

这篇关于可以将对象作为值传递给副本构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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