AppDividend
Latest Code Tutorials

References in C++: The Complete Guide

If we declare a variable reference, it can be accessed by either the variable’s name or reference name. It is effortless to declare references as declared by a simple “&.”And can be accessed by a special ‘.’ operator.

References in C++

References in C++ are somewhat similar to pointers but not identical; there is a difference between  References and pointers. References declared a second name to the existing variable. And the symbol “&” is used to declare a variable reference.

Till now, we’ve discussed two basic variable types:

  1. Normal variables, which hold values directly.
  2. Pointer variables hold the address of another value (or null) and can be dereferenced to retrieve the value at that address they point to.

References are the third primary type of variable that C++ supports. The reference is a C++ variable that acts as an alias to another object or value.

C++ supports three kinds of references:

  1. References to non-const values (typically just called “references”, or “non-const references”), which we’ll discuss in this lesson.
  2. References to const values (often called “const references”), which we’ll discuss in the next lesson.
  3. C++11 added r-value references, which we cover in detail in the chapter on move semantics.

Difference between Pointer and Reference

A null pointer is present, but there is nothing like Null Reference as the Null pointer shows that they are not pointing to any variable.

If a reference refers to an object, it can’t be changed to refer to another object. But pointers at any time can refer to any object. So, in a nutshell, references cannot be reseated.

Reference is initialized at the time of creation but pointers anywhere you want.

References will be accessed by ‘.’ operator (Dot operator), but the pointer will be accessed by the ‘->’ operator (Arrow). 

Because of all the reasons, we cannot use References for implementing data structures in C++ For Linked List, Trees, and many more.

Similarities between Pointers and References

Use to change local variables of a function in any other function.

Otherwise, it can’t happen. So it’s a fair use of pointers and references to change the local variables; otherwise, it’s tough to use functions in programs.

Example: Suppose we have made a program to swap two numbers, but we have made a separate function for swapping, and we give parameters to that function from our program

And when both the numbers get swapped, then if we have not used pointers or references, we don’t get swapped results on returning. 

It saves one copy of objects when arguments are passed in function.

*Now, the question arises of how we can create References.

#include<iostream>
using namespace std;
int main()
{
  int x = 10;
  int &ref = x; (Way of declaration)
  ref = 25;
  x = 35;
}

In C++, we pass reference as a function parameter, and references can also be returned in C++ as with any other datatypes.

Powers of References

References in C++ can’t be used for implementing data structures, but in Java can be used for implementing data structures. That’s why Java does not contain pointers and is a powerful programming language.

Application

If the value of any variable gets changed in the function, then it gets changed in the whole program.

See the following program.

#include <iostream>
using namespace std;

void swap(int &a, int &b)
{
    int temp = a;
    a = b;
    b = temp;
}

int main()
{
    int a = 20, b = 30;
    swap(a, b);
    cout << "After Swap Value of a is " << a << " and b is " << b << "\n";
}

See the following output.

 

References in C++ Example

Copy of large structures should be avoided as a new copy is created, there is a wastage of  CPU time and memory, in loops for modifying all objects. Therefore, in loops so that no copy of object occurs.

Avoiding copy of large structures

Let’s take a function that has to receive a large object. If we pass it without reference, a new copy of it is created, which causes a wastage of CPU time and memory. We can use references to avoid this.

See the following code.

#include<iostream>

using namespace std;

struct Employee { 
   string name; 
   string address; 
   int eNo;
};

void main(const Employee &e)
{ 
    cout << e.name << "  " << e.address << "  " << e.eNo;
}

That’s it for this example.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.