Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

References in C++

References in C++

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.
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.

Syntax

data_type &refernce_variabe=value_variable;

Example:

int a=5;
int &z=a;//now z and a have same address
References in C++
  • 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

#include
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
}

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++

void swap (int& first, int& second) //aliasing a and b 
{
int temp = first;
first = second;
second = 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