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

We can declare variables of reference as follows for 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:-

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.

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.

Pin It on Pinterest

Share This