• Advertisement
Sign in to follow this  

Why use reference in copy constructor?

This topic is 1753 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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...)

Edited by The King2

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement