为什么复制构造函数要在 C++ 中通过引用接受其参数?

2021-12-30 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".)

相关文章