Why use reference in copy constructor?

Started by
1 comment, last by noatom 11 years ago

So when a user usually creates a copy constructor for a class he uses a reference: X(X&)

But why? Why a reference and not a value? I know that the reference way is cheaper and faster but still,there must be some other explanation.

Advertisement

Because having value instead of reference/pointer in any function creates a copy of the class object. Which constructor are we overloading again?

(In case you need further explanation: As we pass X by value, this would create a copy which would invoke the copy constructor - which would create another copy, also invoking the copy constructor, and so on...)

basically it would create an infinite loop,thanks :)

This topic is closed to new replies.

Advertisement