为什么复制构造函数应该在 C++ 中通过引用来接受它的参数? [英] Why should the copy constructor accept its parameter by reference in C++?

查看:19
本文介绍了为什么复制构造函数应该在 C++ 中通过引用来接受它的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么复制构造函数的参数必须通过引用传递?

Why must a copy constructor's parameter be passed by reference?

推荐答案

因为如果不是按引用,就是按值.为此,您制作一个副本,并为此调用复制构造函数.但要做到这一点,我们需要创建一个新值,所以我们调用复制构造函数,等等......

Because if it's not by reference, it's by value. To do that you make a copy, and to do that you call the copy constructor. But to do that, we need to make a new value, so we call the copy constructor, and so on...

(您将有无限递归,因为要复制,您需要复制".)

(You would have infinite recursion because "to make a copy, you need to make a copy".)

这篇关于为什么复制构造函数应该在 C++ 中通过引用来接受它的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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