为什么复制构造函数在C ++中通过引用来接受它的参数? [英] Why should the copy constructor accept its parameter by reference in C++?
本文介绍了为什么复制构造函数在C ++中通过引用来接受它的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么必须通过引用传递其参数的副本构造函数
Why must a copy constructor be passed its parameter 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屋!
查看全文