C++ reference
In C++ a reference is nothing but an alternative name for a variable.The only difference is a variable has an address but a reference does not have an address of it’s own.We can make a reference refer to another variable/object by adding the character ‘&‘ in front of the variable name.
int i=90 , //i is a variable &ri=i ; //'ri' is a reference to ‘i’ string st=”string” , //st is a variable &stref=st ; //'stref' is a reference to st
Note the difference between a variable and a reference.Only the character ‘&‘ separates from a variable and reference.
Reference must be always initialized when it is declared
A reference of any type whether it may be an int or char or string type,a reference must be always initialized when it is declared.If you fail to initialize it during it’s declaration you will get an error.
Link Variable declaration,initialization
int &ref ; //error! 'ref' not initialized string &sref ; //error! sref not initialized
But why is initialization of reference necessary when it is declared? As said earlier a reference is just an alternative name for a variable.And a name cannot exist alone,it must be always associated with some memory or variable.For instance in our society,we can consider reference as a name given to a person.A name will only exist only if the person with that name exists.Giving name to some nonexistent person is an absurd.Like wise declaring a reference without associating it to any variable is an absurd idea.
Accessing the value of a variable using a reference
We can access the value of the variable the reference refer to by simply using the reference name without the ‘&‘ sign.If we use the ‘&‘ sign we will get the address of the variable which it refers to.And surprisingly we can also get the address value of any variable by adding the sign ‘&’ in front of the variable name.
int i=90 ,&ri=i ; cout<< ri << ” ” << (int)&ri << endl ; cout<< (int)&i ;
The output in my computer is,
90 6946536
6946536
The address value given by &ri and &i is the same.
*Note the actual address value is represented in hexadecimal format but here it is casted to int type so the address is in integer format.
Link :What is casting
Initializing a reference
Since a reference can only refer to another variable we cannot initialize a reference directly with a literal( a value).However,we can assign a value to a reference only after it is referring to another variable.We can also use reference to initialize another variable or another reference.Look at the code below.>
int &rr=89 ; //error!! direct initialization int i=90 , &ri=i ; ri=654 ; //Works fine,assign after referring to 'i' cout<< ri << ” ” << i << endl; int ii=ri , //works fine &rii=ri ; //works fine,a reference is used as an initializer cout<< ii << ” ” << rii ;
Output,
654 654
654 654
Since 654 is assigned to ri,the value of the variable referred by ri namely the variable ‘i’ value will also change accordingly.The first output of the above code is 654 654 ,meaning the value of ‘i’ is also changed.And since the variable ‘ii’ and reference rii are initialize with ri reference whose value is 654 the second output is also 654 654.
A reference can refer to only similar type variable
We cannot make a reference of one type refer to a variable of another type,if we try to do so we will get an error.This is because a reference can refer to only one type.
char c=’9′ ; int &r=c ; //error, 'r' is int type and 'c' is char type