Reference Types
The symbol "&" is interpreted as an address operator as well as AND operator. This operator is also used to declare a "reference variable". A reference is referred to an alias, serves as an alternative name for the object.
e.g.
int ival = 1024;
int &rval = ival; // rval is reference to ival
- A reference variable should be initialized at time of declaration itself.
e.g.
int &iref; // wrong
int &iref = ival //o.k.
- Once a reference has been made to a variable, it cannot be altered to refer to another variable.
e.g.
void main()
{
int num1 = 10, num2 = 200;
int &ref = num1;
cout<< num1 << num2 << ref ; //10,200,10;
ref = num2;
cout << num1 << num2 << ref ; //200,200,200
}
It changes the contents of the original variable, which is not the desired action.