Categories: C++

References in C++

C++ references allow you to give a variable a second name that you can use to read or modify the original data stored in that variable. Once a reference has been initialized with a variable, it can be referred to using either the variable name or the reference name. For instance,

int x=19;
int &a=x; //here a is a reference to int x.

A variable can be declared as a reference by including ‘&’ in the declaration, and there is no need to use the ‘*’ to dereference a reference variable.

Pointers vs. References:

References and pointers are frequently confused, but there are three major differences between the two.

  • NULL references are not permitted. You must always be able to assume that a reference refers to a valid piece of storage.
  • Once a reference is set to an object, it cannot be changed to another object. At any time, pointers can be directed to another object.
  • When a reference is created, it must be initialized. At any time, pointers can be initialized.
Functions with references:

When a function receives a variable reference, it can change the variable’s value.

Note: When we return a reference from a function, whatever the reference is connected to should not be out of scope when the function ends. Make that global or static.

Example:
#include <iostream>
using namespace std;

void swap(int& x, int& y)
{
 int temp = x;
 x = y;
 y = temp;
}

int main()
{
 int a = 2, b = 3;
 cout <<"Before swapping: "<< a << " " << b;
 swap(a, b);
 cout<<"After swapping: " << a << " " << b;
 return 0;
}
Output:
Before swapping: 2 3
After swapping: 3 2
Const Reference:

Consider a function that must accept a large object. If we pass it without a reference, a new copy is created, wasting CPU time and memory. To avoid this, we can use const references. For instance,

void func(const int& x)
{ 
    x++; 
}   // ERROR

int main()
{
    int i=10;
    func(i);
}
Output:
/tmp/RXDruFSvQc.cpp: In function 'void func(const int&)':
/tmp/RXDruFSvQc.cpp:3:5: error: increment of read-only reference 'x'
    3 |     x++;
      |     ^

Because the argument is passed as a const reference, we cannot change it in the function.

Note:

  • References are less powerful than pointers
  • References are safer and easier to use

Note: also read about Mutable keyword in C++

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Recent Posts

What is object oriented design patterns

A design pattern is a reusable solution to a commonly occurring problem in software design. They…

5 months ago

Factory Method Design Pattern in OODP

Factory Method is a creational design pattern that deals with the object creation. It separates…

5 months ago

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

11 months ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

11 months ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

11 months ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

12 months ago