Confusion in const and &... | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 9

Confusion in const and &...

Did anyone notice in the syntax for copy constructor... class_name(const class_name &obj_name) const and reference is used at the same time...I get that we dont wanna change the original argument passed to the ctor that's why we used the const but what about the reference...the only reason of using it would be so that we can CHANGE the original argument...! Doesn't const and reference contradicts each other ._0

26th Jan 2018, 5:36 PM
Cyberspace Cyborg
Cyberspace Cyborg - avatar
2 Answers
+ 6
Thanks that clears a lot of the confusions :)
27th Jan 2018, 9:34 AM
Cyberspace Cyborg
Cyberspace Cyborg - avatar
+ 2
The other reason we would pass by reference is speed. By using a constant reference we're telling the user of the function "we're using references just for speed, don't worry about side effects".
26th Jan 2018, 5:40 PM
Vlad Serbu
Vlad Serbu - avatar