C++ References
In this page:
data_type &reference_name = variable_name; // an alias for variable_name
What is a Reference?
A reference in C++ is an alias for an existing variable, sharing the exact same memory address as the original, so once created, any change made through the reference directly updates the variable it refers to.
Unlike a pointer, a reference cannot be reseated to refer to something else later.
Example: What is a Reference?
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare value and set it to 5
int value = 5;
int &ref = value;
ref = 10;
// Print to the console with cout
std::cout << value << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Modifying Variables via References
Because a reference shares its target's memory location, modifying the reference is indistinguishable from modifying the original variable — there's no separate copy to keep in sync.
This makes references useful whenever you want an alternate name for the same piece of data.
Example: Modifying Variables via References
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare score and set it to 50
int score = 50;
int &scoreRef = score;
scoreRef += 20;
// Print to the console with cout
std::cout << score << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Pass by Reference
You can pass arguments to functions by reference, which avoids copying large variables or objects and lets the function update the caller's original arguments directly.
This is typically faster than passing by value for anything larger than a primitive type.
Example: Pass by Reference
// Include std::cout and std::cin
#include <iostream>
void increase(int &x) {
x += 1;
}
// Program execution starts in main()
int main() {
// Declare num and set it to 5
int num = 5;
increase(num);
// Print to the console with cout
std::cout << num << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Constant References
If you want the performance benefit of reference passing without letting the function modify your data, pass a const reference. It avoids the cost of copying large objects while still guaranteeing the caller's original value stays untouched.
Example: Constant References
#include <iostream>
#include <string>
void printName(const std::string &name) {
std::cout << name << std::endl;
// name = "Changed"; // compile error
}
int main() {
printName("Alex");
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Returning References from Functions
Functions can return references, which is useful for chaining method calls or exposing a modifiable slot inside a container.
Be careful never to return a reference to a local variable, since it will dangle and point to invalid memory the instant the function returns.
Example: Returning References from Functions
// Include std::cout and std::cin
#include <iostream>
int scores[3] = {10, 20, 30};
int &getScore(int index) {
// Send scores[index] back to the caller
return scores[index];
}
// Program execution starts in main()
int main() {
getScore(1) = 99;
// Print to the console with cout
std::cout << scores[1] << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Declaring a reference without initializing it, such as
int& r;, which is a compile error because a reference must bind at declaration. - Expecting
r = y;to make the reference refer toy, when it copies the value ofyinto the original variable. - Returning a reference to a local variable, which no longer exists after the function ends.
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: