×

C++ References

C++ References

An alias is a reference variable that is another name for a variable already in existence. If a relation is initialized with a variable, it is possible to use either the important variables or the relation name to refer to the variable.

References vs. Pointers

References are always confused with indicators, but there are three significant differences between references and indicators:-

  • You are not allowed to have NULL references. You must always be in a position to imagine that comparison is linked to a legitimate storage part.
  • Upon initialization of a reference to an entity, it could not be changed to refer to another object. At any instance of time points may be pointing to another thing.
  • When it is generated, a reference has to be initialized. Points should also be initialized.

Creating References in C++

Find a word for the variable as a label attached to the location in memory of the variable. Then you can think of a reference as a second label attached to that place of memory. Thus, either the original variable name or the reference can be used to access the contents of the variable. For example, suppose we have the example below:-

int k = 20;

We can declare variables of reference as follows for k.

int& s = k;

Read the & as a response in those declarations. Hence, read the first declaration as "s is an integer description configured to k and read the second statement as" s would be an optimized double definition to t." Take the following example with references on int and double:-

#include <iostream>
using namespace std;
int main () {
   // declare simple variables
   int    k;
   double t;
   // declare reference variables
   int&    s = k;
   double& a = t;
   k = 8;
   cout << "Value of k : " << k << endl;
   cout << "Value of k reference : " << s  << endl;
   t = 15.2;
   cout << "Value of t : " << t << endl;
   cout << "Value of t reference : " << a  << endl;
   return 0;
}

Output:

C++ References

Example no. 2:-

#include<iostream>

using namespace std;

int main()

{

  int y = 20;

  // ref is a reference to y.

  int& refr = y;

  // Value of y is now changed to 20

  refr = 40;

  cout << "y = " << y << endl ;

  // Value of y is now changed to 30

  y = 25;

  cout << "refr = " << refr << endl ;

  return 0;

}

Output:

C++ References

Applications

Modify the passed parameters in a function: When a function obtains a variable reference, it can change the data values. For example, inputs are switched using references in the following program.

#include<iostream>
using namespace std;
void swap (int& firstname, int& secondname)
{
    int temp = firstname;
    firstname = secondname;
    secondname = temp;
}
int main()
{
    int j = 4, k = 8;
    swap( j, k );
    cout << j << " " << k;
    return 0;
}

Output:

C++ References

Avoiding a copy of large structures: Picture a device that has a broad object to obtain. If we pass that without regard, a new copy of it will be created, which will cause CPU memory and time wastage. To stop this, we should use the references.

References are less powerful than pointers.

  • The reference to some other attribute may not be made later whenever a comparison is formed; it could not be repartitioned. Often this is achieved using pointers.
  • References are not NULL. Points are also rendered NULL to show they do not point to something important.
  • Upon declaration, a reference must be configured. There is no such restriction with pointers because of the limitations above, references in C++ cannot be used to enforce data structures such as Linked List, Tree. In Java, examples have no limitations above and can be used to implement all data structures. References being stronger in Java is the primary reason that Java does not need pointers.

Related Topics

C++ STL Components

C++ STL Components In today’s article, we are going to learn about all the points things that is related to STL in C++ so stay connected because you are going to...

6 minutes read.

Vector in C++

Vector in C++ In today’s article, we will be learning all the things about vector in C++ and how vector is different form an array in C++. So basically, vector is very...

3 minutes read.

C++ Scope of Variables

In this tutorial, we will explore about the scope of variables in c++ programming language. And also, how it works in a program. What is Scope? The range of applications for something...

4 minutes read.

C++ Maximum Index Problem

Given an array A[] of positive integers. We will find the maximum of (j-i) such that i and j are the indexes of A[] and A[i] <= A[j], i<=j For...

5 minutes read.

Function Pointer in C++

Definition: A function pointer in C++ is the same as a usual pointer which is used to point some variables. Function pointers are used to point functions or say store the...

4 minutes read.

10 Best C and C++ Books for Beginners & Advanced Programmers

If you want to become a skilled software developer, you should never stop learning, whether you're a working professional or a student. Why, therefore, only C or C++? The fundamental...

6 minutes read.

Accumulate() and partial_sum() in C++ STL Numeric header

The C++ STL's numeric library includes the numeric header. This library provides efficient numeric arrays, support for random number generation, and fundamental mathematical operations and types. Several of the numeric...

3 minutes read.

List back () function in C++ STL

The list::back () function of the C++ STL returns a direct reference to the last element in the list container. This function varies from list::end (), which just returns an...

2 minutes read.

Vector resize() in C++

Vector resize() in C++ Vectors are called dynamic arrays and can automatically adjust their size when a component is added or deleted. This container is used for storage. The function modifies the...

3 minutes read.

C++ Multimap

Definition: In C++, a multimap is similar to a map with the additional concept, where multiple elements possess the same keys. It is also not necessary that the key values and...

4 minutes read.

Differences between Local and Global Variable

Define Global Variable Global variables are those that may be accessible worldwide across a programme and are defined outside of any functions or blocks. It may be accessed by any function in...

3 minutes read.

Array of Vectors in C++ STL

Prerequisites: C++ STL Arrays and C++ STL Vector. A group of items kept in consecutive memory region is known as an array. It is to group similar objects of the same...

4 minutes read.

C++ Program For FCFS (First Come First Serve)

The most basic scheduling technique is FCFS, often known as "FIFO (First In, First Out)". In this procedure, the first one is utilized and executed first, while the second one...

4 minutes read.

How to create a button in C++

A button is an option through which a user can control to click a provided input to an application. There are several buttons, each with different styles, to maintain the...

3 minutes read.

C++ add two numbers using the function

Here we will learn how to add two numbers by creating function in C++. Let’s learn this with help of example. Code: - #include <iostream> using namespace std; int add_two_no(int a, int b); int main(){   int...

1 minute read.

Converting string into integer in C++

When programming in C++, we'll frequently need to change one data type to another. When we use C++ to create apps, we must transform data from one type to another. When...

7 minutes read.

External merge sort in C++

External merge sort in C++ External sorting is a concept for a group of sorting algorithms capable of handling large data volumes. External sorting is needed if the information getting sorted...

9 minutes read.

C++ Program to Implement Shell Sort

    C++ Program to Implement Shell Sort shell sort is basically an Insertion Sort variant. In the insertion sort, we only transfer elements ahead of one location. Many movements are involved...

2 minutes read.

Constructor Overloading in C++

A Constructor is a class member function that is used to initialize the class's objects. Constructors have no return type and are called automatically when an object is formed. Constructor Characteristics Constructors...

3 minutes read.

New Operator in C++

Dynamic memory allocation in C++ means manually allocating the memory by the developer duing run-time. The dynamic memory is allocated in the heap section of the RAM, whereas the static...

3 minutes read.