+ 1

What is the use of reference variable and why we use it?

23rd May 2019, 3:39 AM
Rupali Patel
Rupali Patel - avatar
7 Answers
+ 6
E.g. to be able to pass it into a function and change its value there.
23rd May 2019, 4:45 AM
Sonic
Sonic - avatar
+ 4
A reference is not a variable! Reference is like the c++ style pointers. They act a bit similar but references are safer. ~ swim ~ explained well the difference. Under the hood a reference is usually implemented with pointer but this is only compiler dependant. Reference behaviour is well defined by the standards.
23rd May 2019, 7:16 AM
AZTECCO
AZTECCO - avatar
+ 4
~swim~ , sonic and AZTECCO Thank you so much
23rd May 2019, 7:29 AM
Rupali Patel
Rupali Patel - avatar
+ 2
We can do it by pointer too then why we need it?
23rd May 2019, 6:02 AM
Rupali Patel
Rupali Patel - avatar
+ 1
rupali patel references allow you to write cleaner code,pointers make it hard to write elegant code plus they have lots of problems associated with them. Plus references help avoid passing copies of objects to functions,saving you space and time.Eg Passing a vector of 100M items to a function by copy can be avoided by using a reference.
23rd May 2019, 11:11 AM
Mensch
Mensch - avatar