Jump to content

View more

Image of the Day

WIP title screen for #DeathOfAPartisan #screenshotsaturday #gamedev https://t.co/qJNhfZCvd4
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.


Sign up now

Why use reference in copy constructor?

4: Adsense

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   

925
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.



#2 Juliean   Members   

6870
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   

925
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.