Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why do copy constructors have to be passed by reference?

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi I've been trying to figure this one out. Could someone please explain to me in lay-men terms why copy constructors have to be passed by reference and not by value. Thanks


 
Well you see:
in any function, passing an object argument by value would call the copy constructor of this object's class to make a copy of the argument to be used in the function.

it's no different for the copy constructor function, passing the argument by value would call the copy constructor who's argument is passed by value, so it needs to call the copy constructor which then calls the copy constructor......and so on. the process would fall in an infinite loop.

therefor the argument should be passed by value to avoid its being copied

hope this answers your question
 
sorry i meant:
therefor the argument should be passed by REFERENCE to avoid its being copied
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top