为什么复制构造函数的参数必须为引用呢,原因如下:
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 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".)
来源:
http://stackoverflow.com/questions/2685854/why-should-the-copy-constructor-accept-its-parameter-by-reference-in-c
老忘记,举个例子也许更好理解:
//创建Type类型的对象a
Type a;
//对a的对象内部数据成员赋值
// 调用复制构造函数创建对象b
Type b( a )
由于b的复制构造函数的参数被定义为值传递,那么首先会创建一个Type的临时变量,假设为temp1,然后再传递给b的复制构造函数
Type temp1( a )
由于temp1的复制构造函数的参数被定义为值传递,那么首先会创建一个Type的临时变量,假设为temp2,然后再传递给b的复制构造函数
Type temp2( a )
类推,会出现temp3,temp4,...直至无穷多!
所以复制构造函数的参数必须为引用