Professional Documents
Culture Documents
2.2. References
REFRENCES: A variable is declared as reference when it becomes an alternative name for an existing
variable.
A variable can be declared as reference by putting ‘&’ in the declaration.
A reference variable provides an alias (alternative name) for a previously defined variable.
A reference variable is created as follows:
Data-type & reference-name = variable-name
EXAMPLE:
Float total = 100;
Float & sum = total;
cout<<total;
cout<<sum;
//both will give same result
total = total+10; // this will change value of total and sum both
sum = 0; // this will also change the value of both sum and total
int x;
int *p = &x;
int & m = *p;
int & n = 50;
One important application of this pointer is to return the object it points to. For example
return *this;
inside a member function will return the object that invoked the function