Jump to content

  • Log In with Google      Sign In   
  • Create Account


Why use reference in copy constructor?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 noatom   Members   -  Reputation: 782

Like
0Likes
Like

Posted 03 April 2013 - 11:50 AM

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.



Sponsor:

#2 Juliean   GDNet+   -  Reputation: 2350

Like
3Likes
Like

Posted 03 April 2013 - 11:55 AM

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, 03 April 2013 - 11:58 AM.


#3 noatom   Members   -  Reputation: 782

Like
0Likes
Like

Posted 03 April 2013 - 12:14 PM

basically it would create an infinite loop,thanks :)






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS