Sign in to follow this  
noatom

Why use reference in copy constructor?

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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this