So far, we have learned about the two types of variables supported by C++:
-
Ordinary variables are variables that hold values of a certain type. For example, creating an integer variable means that the variable can hold integer values.
-
Pointers are variables that store the address of another variable. You can retrieve the value pointed to by a pointer by dereferencing it.
-
In C++, there is another type of variable called a reference. A reference acts as an alias for another variable.
How to Create a Reference?
You can create a reference using the ‘&’ operator. When we create a variable, it occupies a certain memory location. We can create a reference to that variable, allowing us to access the original variable using either its name or the reference. For example:
int a = 10;
Now, we create a reference variable for the above variable.
int &ref = a;
The statement above means that ‘ref’ is a reference variable for ‘a’, which means we can use the ‘ref’ variable in place of the ‘a’ variable.
👇 Click to Claim 👇
👉 C Language Knowledge Resource Collection
C++ Provides Two Types of References:
-
Reference to non-constant values
-
Reference as an alias
Reference to non-constant values
It can be declared using the ‘&’ operator with reference type variables.
#include <iostream>
using namespace std;
int main() {
int a = 10; // Variable initialization
int &value = a;
std::cout << value << std::endl;
return 0;
}
Output:
10
Reference as an Alias
A reference as an alias is another name for the referenced variable.
For example:
int a = 10; // 'a' is a variable.
int &b = a; // 'b' references 'a'.
int &c = a; // 'c' references '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;
}
In the code above, we created a variable ‘a’ that contains the value ’70’. We declared two reference variables ‘b’ and ‘c’, both referencing the same variable ‘a’. Thus, we can say that the ‘a’ variable can be accessed through the ‘b’ and ‘c’ variables.
Output:
Value of a is: 70Value of b is: 70Value of c is: 70
Characteristics of References
References have the following characteristics:
Initialization
Must be initialized at the time of declaration.
#include <iostream>
using namespace std;
int main() {
int a = 10; // Variable initialization
int &b = a; // b references a
std::cout << "Value of a is: " << b << std::endl;
return 0;
}
In the code above, we created a reference variable ‘b’. At the time of declaration, ‘a’ was assigned to ‘b’. If no assignment was made at the time of declaration, the code would look like this:
int &b;
&b = a;
The code above will cause a compile-time error because no assignment was made at the time of declaration.
Output:
Value of a is: 10
Reassignment
Cannot be reassigned, meaning the reference variable cannot be modified.
#include <iostream>
using namespace std;
int main() {
int x = 11; // Variable initialization
int z = 67;
int &y = x; // y references x
int &y = z; // y references z, but this will throw a compile-time error.
return 0;
}
In the code above, the reference variable ‘y’ references the variable ‘x’ and then assigns ‘z’ to ‘y’.However, reference variables do not allow reassignment, which will lead to a compile-time error.
Compile-Time Error:
main.cpp: In function 'int main()':
main.cpp:18:9: error: re-declaration 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 passed as function parameters. It does not create a copy of the parameter but acts as an alias for the parameter. This improves performance as no copy of the parameter is created.
Let’s understand this through a simple example.
#include <iostream>
using namespace std;
int main() {
int a=9; // Variable initialization
int b=10; // Variable initialization
swap(a, b); // Call function
std::cout << "The value of a is: " << a << std::endl;
std::cout << "The 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;
}
In the code above, we swapped the values of ‘a’ and ‘b’.We passed the variables ‘a’ and ‘b’ to the swap() function.In the swap() function, ‘p’ references ‘a’ and ‘q’ references ‘b’.When we swap the values of ‘p’ and ‘q’, we are also swapping the values of ‘a’ and ‘b’.
Output
The value of a is: 10The value of b is: 9
Using references as shortcuts
With references, we can easily access nested data.
#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;
}
In the code above, we attempted to access the ‘id’ of the profile structure of the employee structure.Typically, we access this member using the statement e.p.id, but if we need to access this member multiple times, it can be cumbersome.To avoid this, we created a reference variable ‘ref’, which is another name for ‘e.p.id’.
Output
34
Popular Recommendations
-
CLion Tutorial – IDE Performance Monitor in CLion
-
C Language Algorithm – “Minimum Path Sum” Algorithm Problem
-
C++ Tutorial – Detailed Explanation of Null Pointers in C++