Chapter Two: References in C++
Unlike pointers, references are used as aliases in C++. a reference is used to refer to a variable present in the existing code. When you initialize a reference and assign it to a variable in the code, you can use the variable itself or the reference variable to call the value stored in the variable if you need to use it in a different function.
Difference Between References and Pointers
People often confuse themselves when it comes to references and pointers. There are three differences between the two:
-
As mentioned in the previous chapter, you can have a null pointer, but you cannot have a null reference in your code. Make sure the reference is always tagged to a variable or function which has a return value.
-
When you initialize and assign a reference to a specific object, you cannot change its value to another object in the code at any point. You can use pointers to look at different objects at varied points in the code.
-
Every reference needs to be initialized before it is tagged to any variable. Unlike pointers, you cannot initialize a reference in any line of the code.
How to Create References
From the first book, you know that every variable has a name. Let us assume that this name is the label attached to the location of the variable’s value in the memory. When you tag a reference to the variable, it becomes the second label attached to the location. Therefore, you can refer to the value in the memory location using either the reference or the original variable name. Let us consider the following example:
// Initialize a variable ‘i’ and assign it a value
int i = 4;
float j = 2.8;
// Declare a reference variable in your code for the above variable
int &r1 = i;
float &r2 = j;
The ampersand (&) in the above line is your reference. Read the above two lines of code as follows:
-
The integer reference, r1, has been initialized and tagged to the variable i
-
The integer reference, r2, has been initialized and tagged to the variable j
In the following example, we look at how you can use references on variables with the data types double and int.
#include <iostream>
using namespace std;
int main () {
// The following statements are used to declare the simple variables in the code
int i;
double d;
// The following statements are used to declare and assign the reference variables to the simple variables
int& r = i;
double& s = d;
i = 5;
cout << "Value of i : " << i << endl;
cout << "Value of i reference : " << r << endl;
d = 11.7;
cout << "Value of d : " << d << endl;
cout << "Value of d reference : " << s << endl
;
return 0;
}
When you compile the above code, you will obtain the following output:
Value of i : 5
Value of i reference : 5
Value of d : 11.7
Value of d reference : 11.7
Coders often use references as function return values or argument lists. The following are two points to bear in mind when you write code in C++:
S. No.
|
Concept
|
Description
|
1
|
Using references as function parameters
|
You can pass references as parameters in functions. It is safer to use them as parameters instead of using simple variables
|
2
|
Using references as function values
|
References can be used like other parameters or data types as return values
|