0
copy constructor
I have just learnt about copy constructor today. I found a syntax of it on internet classname (const classname &obj) { // body of constructor } Why do we use const classname &obj instead of classname &obj ?
1 Answer
+ 10
It is fine to omit the const modifier if you do not plan to pass temporary objects into the copy constructor. A constant reference parameter allows you to pass temporary objects to a function/method.
http://www.cplusplus.com/forum/general/44221/
This also makes sure that you don't accidentally modify your class object which is passed into the copy constructor. As for why class objects are encouraged to be passed by reference:
https://stackoverflow.com/questions/18347109/c-pass-by-value-with-non-primitive-types