开发者

Copy constructor [duplicate]

This question already has answers here: Closed 12 years ago.

Possible Duplicate:

Why should the copy constructor accept its parameter by reference in C++?

开发者_运维知识库

Why the argument to a copy constructor is passed by reference?


If it is passed by value it would require making a copy using a...COPY CONSTRUCTOR. :-)


You can't pass it by value because pass-by-value implies making/passing a copy of the thing ... making a copy of the parameter passed to the copyy constructor would be recursive, cause a stack overflow.


As others have mentioned you can't pass it by value - because then you'd need a copy to create a copy!

The only other alternative would be to pass by pointer, but the syntax would require address-of, like so:

MyClass copy(otherclass);  // by reference
MyClass copy(&otherclass); // by pointer
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜