Reference (C++)
A reference is an alias — another name for an existing variable. Once bound, it always refers to the same variable, so changing the reference changes the original.
C++
References are declared with & and must be initialized when created; they cannot be reseated to refer to something else later. Unlike pointers, they can never be null and need no dereferencing.
References are most commonly used to pass arguments into functions without copying them, especially with const for efficiency.
Syntax
type& name = variable;Examples
C++
#include <iostream>
void addOne(int& n) {
n = n + 1;
}
int main() {
int x = 5;
addOne(x);
std::cout << x << std::endl;
return 0;
}Output:
6