References In C++

Share Your Love

We’ve learned that C++ supports two sorts of variables so far:

An ordinary variable is one that holds the value of a certain type. For example, let’s say we make a variable of type int, which means it can hold an integer value.

A variable that stores the address of another variable is known as a pointer. It can be dereferenced to find out what value this pointer refers to.

C++ also provides references, which are a type of variable. It’s a variable that acts as a placeholder for another variable.

How to create a reference?

The ampersand (&) operator can be used to generate a reference. When we create a variable, it takes up some memory space. We can make a reference to the variable, which allows us to retrieve the original variable using either the variable’s name or the reference. As an example,

int a=10;

Now we’ll make the above variable’s reference variable.

int &ref=a;

The preceding phrase denotes that ‘ref‘ is a reference variable of ‘a,’ implying that we can substitute the ‘ref‘ variable for the ‘a‘ variable.

C++ provides two types of references:

  • References to non-const values
  • References as aliases

References to non-const values:

It can be declared with the reference type variable and the & operator.

#include <iostream>  
using namespace std;  
int main()  
{  
int a=10;  
int &value=a;  
std::cout << value << std::endl;  
return 0;  
} 

Output:

10

References as aliases:

Another name for the variable that is being referenced is referenced as aliases.

For example,

int a=10;   // 'a' is a variable.  
int &b=a; // 'b' reference to a.  
int &c=a; // 'c' reference to a. 

Let’s look at a simple example:

#include <iostream>  
using namespace std;  
int main()  
{  
  int a=70; // variable initialization  
  int &b=a;  
  int &c=a;  
  std::cout << "Value of a is :" <<a<< std::endl;  
  std::cout << "Value of b is :" <<b<< std::endl;  
  std::cout << "Value of c is :" <<c<< std::endl;  
  return 0;
} 

We create a variable ‘a’ with the value ’70’ in the code above. We’ve defined two reference variables, b and c, both of which refer to the same variable, ‘a.’ As a result, the ‘a’ variable can be accessed by the ‘b’ and ‘c’ variables.

Output:

Value of a is : 70
Value of a is : 70
Value of a is : 70

Properties of References:

The following are the properties of references:

Initializátion:

It must be initialised when the declaration is made.

#include <iostream>  
using namespace std;  
int main()  
{  
int a=10; // variable initialization  
int &b=a; // b reference to a  
std::cout << "value of a is " <<b<< std::endl;  
return 0;  
} 

We’ve generated a reference variable, ‘b’ in the code above. The ‘a’ variable is allocated to ‘b’ at the moment of declaration. If we don’t assign at the beginning of the declaration, the code will look like this:

int &b;  
&b=a;

Because ‘a’ is not assigned at the time of declaration, the above code will fail at build time.

Output:

value of a is 10 

Reassignment:

The reference variable can’t be reassigned, which means it can’t be changed.

#include <iostream>  
using namespace std;  
int main()  
{  
  int x=11; // variable initialization  
  int z=67;  
  int &y=x; // y reference to x  
  int &y=z; // y reference to z, but throws a compile-time error.  
  return 0;
}  

The ‘y’ reference variable refers to the ‘x’ variable in the above code, and then ‘z’ is assigned to ‘y’. However, because this reassignment is not allowed with the reference variable, a compile-time error occurs.

Output:

main.cpp: In function 'int main()':  
main.cpp:18:9: error: redeclaration of 'int& y'  
int &y=z; // y reference to z, but throws a compile-time error.  
^  
main.cpp:17:9: note: 'int& y' previously declared here  
int &y=x; // y reference to x  
^  

Function Parameters:

References can also be given as an argument to a function. It acts as an alias for a parameter and does not make a copy of the argument. It improves performance because it does not duplicate the argument.

Now let’s understand with a simple example:

#include <iostream>  
using namespace std;  
int main()  
{  
int a=9; // variable initialization  
int b=10; // variable initialization  
swap(a, b); // function calling  
std::cout << "value of a is :" <<a<< std::endl;  
std::cout << "value of b is :" <<b<< std::endl;  
return 0;  
}  
void swap(int &p, int &q) // function definition  
{  
int temp; // variable declaration  
temp=p;  
p=q;  
q=temp;  
}  

The values of ‘a’ and ‘b’ are swapped in the code above. The variables ‘a’ and ‘b’ have been provided to the swap() function. ‘p’ stands for ‘a’ and ‘q’ stands for ‘b’ in the swap() function. When the values of ‘p’ and ‘q’ are swapped, the values of ‘a’ and ‘b’ are swapped as well.

Output:

value of a is :10 
value of b is :9 

References as shortcuts:

We can simply access the nested data with the use of references.

#include <iostream>  
using namespace std;  
struct profile  
{  
int id;  
};  
struct employee  
{  
profile p;  
};  
int main()  
{  
employee e;  
int &ref=e.p.id;  
ref=34;  
std::cout << e.p.id << std::endl;  
}  

We’re trying to get the ‘id’ of the employee’s profile struct in the code above. We usually access this member with the statement e.p.id, however, if we have any access to this member, this would be a tedious operation. We establish a reference variable, ref, which is another name for ‘e.p.id’, to prevent this issue.

Output:

34

Share Your Love
Avatar photo
Lingaraj Senapati

Hey There! I am Lingaraj Senapati, the Founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.

Articles: 355

Newsletter Updates

Enter your email address below to subscribe to our newsletter