A reference is nothing but an alternative name for a variable,so it 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.
&ri=i ; ///ri is a reference to ‘i’
string st=”string” ,
&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:an int or char or string reference must be always initialized when it is declared.If you fail to initialize it during it’s declaration you will get an error.
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/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.
int &ref ; ///error ,ref not initialized
string &sref ; ///error
Accessing the value using a reference
We can access the value 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 so,
cout<< ri << ” ” << (int)&ri << endl ;
cout<< (int)&i ;
The output in my computer is,
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 to a literals or values.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 i=90 , &ri=i ;
ri=654 ; ///Works fine
cout<< ri << ” ” << i << endl;
int ii=ri ,
&rii=ri ; ///works fine
cout<< ii << ” ” << rii ;
Since 654 is assigned to ri,the value of the variable referred by ri -the variable ‘i’- will also change correspondingly.So 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.
int &r=c ; ///error, r is int type and c is char type
Stop wasting time,earn money($$$) from your website-Join Now!