+ 1
Why Reference( & ) ?
I saw friend in c++, in the note section it written as," Note you must pass reference(&) to the .....", so why to pass reference ???😢😢 Here's the link ---- https://www.sololearn.com/learn/CPlusPlus/1899/
3 Respostas
+ 3
Adding to that performance gain, references allow you to modify the variable also outside the function.
+ 1
a reference is kind of like a pointer, normaly if you pass something to a function, it gets copied into the variable/parameter that the function uses, but if you use a reference the object doesn‘t get copied but instead only the reference to the object, this can save time, since objects are pretty big and more importantly in this case every modification you make to the object(for example changing a member) will be visible from outside the function, which it wouldn‘t be if the object was copied into the function.
+ 1
Without passing reference, the member variables have to be be all copied. It wastes memory and takes much time.