Javatpoint Logo
Javatpoint Logo

C++ References

Till now, we have read that C++ supports two types of variables:

  • An ordinary variable is a variable that contains the value of some type. For example, we create a variable of type int, which means that the variable can hold the value of type integer.
  • A pointer is a variable that stores the address of another variable. It can be dereferenced to retrieve the value to which this pointer points to.
  • There is another variable that C++ supports, i.e., references. It is a variable that behaves as an alias for another variable.

How to create a reference?

Reference can be created by simply using an ampersand (&) operator. When we create a variable, then it occupies some memory location. We can create a reference of the variable; therefore, we can access the original variable by using either name of the variable or reference. For example,

Now, we create the reference variable of the above variable.

The above statement means that 'ref' is a reference variable of 'a', i.e., we can use the 'ref' variable in place of '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 by using & operator with the reference type variable.

Output

 10 

References as aliases

References as aliases is another name of the variable which is being referenced.

For example,

Let's look at a simple example.

In the above code, we create a variable 'a' which contains a value '70'. We have declared two reference variables, i.e., b and c, and both are referring to the same variable 'a'. Therefore, we can say that 'a' variable can be accessed by 'b' and 'c' variable.

Output

 Value of a is :70 
Value of b is :70 
Value of c is :70 

Properties of References

The following are the properties of references:

Initializátion

It must be initialized at the time of the declaration.

In the above code, we have created a reference variable, i.e., 'b'. At the time of declaration, 'a' variable is assigned to 'b'. If we do not assign at the time of declaration, then the code would look like:

The above code will throw a compile-time error as 'a' is not assigned at the time of declaration.

Output

 value of a is 10 

Reassignment

It cannot be reassigned means that the reference variable cannot be modified.

In the above code, 'y' reference variable is referring to 'x' variable, and then 'z' is assigned to 'y'. But this reassignment is not possible with the reference variable, so it throws a compile-time error.

Compile-time error

Function Parameters

References can also be passed as a function parameter. It does not create a copy of the argument and behaves as an alias for a parameter. It enhances the performance as it does not create a copy of the argument.

Let's understand through a simple example.

In the above code, we are swapping the values of 'a' and 'b'. We have passed the variables 'a' and 'b' to the swap() function. In swap() function, 'p' is referring to 'a' and 'q' is referring to 'b'. When we swap the values of 'p' and 'q' means that the values of 'a' and 'b' are also swapped.

Output

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

References as shortcuts

With the help of references, we can easily access the nested data.

In the above code, we are trying to access the 'id' of the profile struct of the employee. We generally access this member by using the statement e.p.id, but this would be a tedious task if we have multiple access to this member. To avoid this situation, we create a reference variable, i.e., ref, which is another name of 'e.p.id'.

Output

 34 

Next TopicC++ Tutorial




Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA