C++ References
In C++, references are an alias for another variable. Unlike pointers, references cannot be null and must always refer to an object. They provide an elegant way to pass data to functions without the overhead of copying large objects, making them an essential feature of C++ programming.
In this guide, we’ll explore:
Let’s dive into C++ references!
A reference in C++ is essentially an alias for another variable. It provides an alternative name to an existing variable, allowing you to access the same memory location. Once a reference is initialized, it cannot be changed to refer to another variable.
data_type& reference_name = variable_name;
int
, float
, char
).
int num = 10;
int& ref = num; // 'ref' is a reference to 'num'
In the above example:
ref
is a reference to the variable num
, meaning ref
and num
refer to the same memory location.ref
will affect num
, and vice versa.While both pointers and references can be used to refer to other variables, they have some key differences:
Feature | Reference | Pointer |
---|---|---|
Initialization | Must be initialized when declared | Can be initialized or left uninitialized |
Nullability | Cannot be null | Can be null |
Re-assignment | Cannot be reassigned once set | Can point to different variables |
Syntax | data_type& ref |
data_type* ptr |
#include <iostream>
using namespace std;
int main() {
int num1 = 10;
int num2 = 20;
int& ref = num1; // Reference to num1
int* ptr = &num1; // Pointer to num1
ref = num2; // Changes the value of num1 to num2's value (20)
cout << "num1: " << num1 << ", ref: " << ref << endl; // num1 and ref are 20
ptr = &num2; // Now ptr points to num2
cout << "num2: " << num2 << ", *ptr: " << *ptr << endl; // Output: num2 and *ptr are 20
return 0;
}
Output:
num1: 20, ref: 20
num2: 20, *ptr: 20
Explanation:
ref
changes the value of num1
because it is an alias for num1
.ptr
can be reassigned to point to num2
.References are often used to pass arguments to functions. This is beneficial because it avoids the overhead of copying large objects. Additionally, references allow the function to modify the argument directly.
#include <iostream>
using namespace std;
// Function to swap two numbers using references
void swap(int& a, int& b) {
int temp = a;
a = b;
b = temp;
}
int main() {
int x = 5, y = 10;
cout << "Before swap: x = " << x << ", y = " << y << endl;
swap(x, y); // Passing by reference
cout << "After swap: x = " << x << ", y = " << y << endl;
return 0;
}
Output:
Before swap: x = 5, y = 10
After swap: x = 10, y = 5
Explanation:
swap
uses references (int& a, int& b
) to directly modify the original variables x
and y
without copying their values.You can also return references from functions. However, this should be done with caution, especially when returning a reference to a local variable, as it may lead to undefined behavior.
#include <iostream>
using namespace std;
// Function that returns a reference to a static variable
int& getStaticValue() {
static int num = 10; // 'static' ensures the variable persists after the function returns
return num;
}
int main() {
int& ref = getStaticValue(); // Get reference to the static variable
cout << "Value: " << ref << endl; // Output: 10
ref = 20; // Modify the value using the reference
cout << "Updated value: " << getStaticValue() << endl; // Output: 20
return 0;
}
Output:
Value: 10
Updated value: 20
Explanation:
getStaticValue
returns a reference to a static
variable. The static keyword ensures that the variable's value persists between function calls.ref
allows the value of the static variable to be modified directly.You can declare references to constant variables. This ensures that the referenced value cannot be modified through the reference.
#include <iostream>
using namespace std;
int main() {
const int num = 100;
const int& ref = num; // Reference to constant
// ref = 200; // Error: cannot modify a constant value
cout << "Value: " << ref << endl; // Output: 100
return 0;
}
Output:
Value: 100
Explanation:
ref
is a reference to a constant integer, meaning you cannot modify the value of num
through ref
.You can also create references to arrays. However, the syntax is slightly different when dealing with arrays compared to basic types.
#include <iostream>
using namespace std;
// Function that takes a reference to an array
void printArray(int (&arr)[5]) {
for (int i = 0; i < 5; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
int main() {
int arr[5] = {1, 2, 3, 4, 5};
printArray(arr); // Passing array by reference
return 0;
}
Output:
1 2 3 4 5
Explanation:
printArray
accepts a reference to an array of size 5. This allows it to modify the array elements directly.