References in C++

References

 

Here, in this page we will discuss about references in C++.
Definition: A variable when declared as a reference becomes an alternate name for the existing variable by pointing to the existing memory instead of creating a new memory. They are also called as alias variables or link variables.
Problem with the pointer is we can change the existing variable address in future and hence insecure.
Reference variables are introduced to overcome this problem.

References in C++

Syntax

data_type &refernce_variabe=value_variable;

Example:

int a=5;
int &z=a;//now z and a have same address
  • In the above example, variables ‘a’ and ‘z’ are pointing to the same memory address
  • So that ‘z’ becomes the alias name for ‘a’
  • Any change in ‘a’ will also reflect change int ‘z’ and vice-versa

Program to demonstate reference variables

Run

#include<iostream>
using namespace std;
main()
{
  int a=10;
  int &b=a;//b is alias of a 
  int &c=b;//c is alias of b
  cout<<"a:"<<a<<" b:"<<b<<" c:"<<c<<endl;
  c=20;//a,b also becomes 20
  cout<<"a:"<<a<<" b:"<<b<<" c:"<<c<<endl;
  cout<<"&a:"<<&a<<" &b:"<<&b<<" &c:"<<&c<<endl;//a,b,c has same address
  return 0;
}

O/P:-

a:10 b:10 c:10
a:20 b:20 c:20
&a:0x6ffe34 &b:0x6ffe34 &c:0x6ffe34
  • Initially ‘b’ is made as an alias of ‘a’ by giving the address of ‘a’ to b now ‘c’ is made an alias of ‘b’, as ‘b’ is pointing a’s address ‘c’ also takes the address of ‘a’.
  • Hence ‘a’, ‘b’, ‘c’ are having the same address and become an alias to each other.
  • Now, ‘b’ is changed to 20, as a result, ‘a’ and ‘b’ also becomes 20 because they are having a common memory space.

 

Differences between Pointer Variables and Reference Variables

Pointer Variables 

  • Initialized anywhere and any number of times
  • Pointer variables have separate memory
  • Pointers are not that secured because of reinitialization
  • It can be void or null

Reference Variables

  • Initialized only once and at declaration only
  • Reference variable will point to the same memory of the value variable
  • They are secured as they are constant  pointers
  • It cannot be void or null

Advantage

When the reference variable is lost it can be accessed through another alias, but if the pointer is lost, the whole security comes into question.

Applications

  • The internet architecture is developed by C++ under call- by-  reference concept
  • Many other applications like Chat dialogue box, we can observe gtalk box used in video conferencing, teleconferencing and live telecast etc.

Call by reference in C++

Run

#include<iostream>
using namespace std;
void swap (int& x, int& y) //aliasing a and b
{
int temp = x;
x = y;
y = temp;
cout<<"x:"<<x<<" y:"<<y<<endl;
}
int main()
{
int a = 2, b = 3;
swap( a, b ); //func_call
cout<<"a:"<<a<<" b:"<<b<<endl;
return 0;
}

O/P

x:3 y:2
a:3 b:2
  • Here instead of passing address through pointers ‘x’, ‘y’ are made aliases to ‘a’, ‘b’
  • As ‘x’ and ‘a’, ‘y’ and ‘b’ both are made to have the same address, change in one affects others