为什么复制构造函数应该在 C++ 中通过引用来接受它的参数?

2022-01-20 00:00:00 复制 constructor c++ copy-constructor

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

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".)

相关文章