In C++, a reference works as an alias for an existing variable, providing an alternative name for it and allowing you to work with the original data directly.
#include <iostream>
using namespace std;
int main() {
int x = 10;
// ref is a reference to x.
int& ref = x;
// printing value using ref
cout << ref << endl;
// Changing the value and printing again
ref = 22;
cout << ref;
return 0;
}
Output
10 22
Explanation: In this program, ref is a reference to the variable x, meaning ref is just another name for x. When the value of ref is modified, it directly changes the value of x, since both ref and x refer to the same memory location.
If you’ve worked with pointers before, references might feel similar, they both refer to the same memory. However, references provide a simpler, more readable way to alias a variable without using *.
To see how references differ from pointers, check out: Pointers vs References.
Syntax
& is used to create a reference to a variable as shown:
T &ref = var;
Here, ref refers to the variable named var which is of type T.
It is important to keep in mind the following things about reference.
- It is necessary to initialize the reference at the time of creation.
- A reference cannot refer to another variable once it is declared.
Applications
There are multiple applications for references in C++, a few of them are mentioned below:
1. Passing Arguments by Reference
References are commonly used in function arguments to allow modification of the original variable passed to the function. They are also more efficient for large data structures since no copies are made.
#include <iostream>
using namespace std;
void modifyValue(int &x) {
// Modifies the original variable
x = 20;
}
int main() {
int a = 10;
// Pass a by reference
modifyValue(a);
cout << a;
return 0;
}
Output
20
Explanation: In this example, the function modifyValue() modifies the value of a directly by using a reference to it. No copy of a is made, and the original variable is modified.
2. Returning Reference from Functions
In C++, functions can return references to variables. This is especially useful when you need to return large data structures or want to allow direct modifications to a variable inside a function.
#include <iostream>
using namespace std;
int& getMax(int &a, int &b) {
// Return the larger of the two numbers
return (a > b) ? a : b;
}
int main() {
int x = 10, y = 20;
int &maxVal = getMax(x, y);
// Modify the value of the larger number
maxVal = 30;
cout << "x = " << x << ", y = " << y;
return 0;
}
Output
x = 10, y = 30
Explanation: Here, the function getMax() returns a reference to the larger of two integers. Since maxVal is a reference, changing maxVal also modifies the value of y.
Note: Keep in mind not to return a reference to local variables as they are destroyed once the function returns some value.
3. Modify Data in Range Based Loops
Range-based for loops generally iterate over elements by value, but using references allows direct modification of the original elements.
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> vect{ 10, 20, 30, 40 };
// We can modify elements if we
// use reference
for (int& x : vect) {
x = x + 5;
}
// Printing elements
for (int x : vect) {
cout << x << " ";
}
return 0;
}
Output
15 25 35 45
Limitations of References
- Once a reference is created, it cannot be later made to reference another object; it cannot be reset. This is often done with pointers.
- References cannot be null. Pointers are often made NULL to indicate that they are not pointing to any valid thing.
- A reference must be initialized when declared. There is no such restriction with pointers.
Due to these limitations, references in C++ are less flexible than pointers and are generally not preferred for implementing data structures like linked lists and trees, where reassignment and nullability are required. In Java, references do not have these restrictions and are used to implement all data structures, which is one of the reasons Java does not require pointers.
Advantages of using References
- Safer: Since references must be initialized, wild references like wild pointers are unlikely to exist. It is still possible to have references that don't refer to a valid location
- Easier to use: References don't need a dereferencing operator to access the value. They can be used like normal variables. The '&' operator is needed only at the time of declaration. Also, members of an object reference can be accessed with the dot operator ('.'), unlike pointers where the arrow operator (->) is needed to access members.
Due to these advantages, references are preferred in certain cases, such as passing arguments to a copy constructor and overloading operators like ++, where using references is more appropriate than pointers.

