Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


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: 785

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: 2691

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: 785

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