C++ references are a powerful feature of the C++ programming language that allow you to create aliases to existing variables.
In this tutorial, we will cover everything you need to know about C++ references, including their syntax, how they differ from pointers, and how to use them in your programs.
Table of Contents
What is a reference in C++?
A reference in C++ is an alias to an existing variable. Once a reference is declared, it can be used in the same way as the original variable.
The main advantage of using references is that they allow you to avoid copying large objects, which can be expensive in terms of memory and time.
Syntax of C++ references
The syntax for declaring a reference in C++ is as follows:
type& ref = var;
Here, type is the data type of the variable being referenced, ref is the name of the reference, and var is the name of the original variable being referenced. The & symbol is used to indicate that ref is a reference.
For example, to create a reference to an integer variable x, you would write:
int& ref = x;
Example of C++ references
Here’s an example that demonstrates the use of references in C++:
#include <iostream> using namespace std; void swap(int& a, int& b) { int temp = a; a = b; b = temp; } int main() { int x = 10; int& ref = x; ref = 20; cout << x << endl; int a = 5, b = 10; cout << "Before swap: a = " << a << ", b = " << b << endl; swap(a, b); cout << "After swap: a = " << a << ", b = " << b << endl; return 0; }
In this example, we first create a reference ref to an integer variable x, and then assign a new value to x through the reference.
We then define a function swap that takes two integer references as arguments and swaps their values. Finally, we call the swap function to swap the values of two integer variables a and b.
Differences between references and pointers
While references and pointers may seem similar at first glance, there are several key differences between them:
- Once a reference is initialized, it cannot be made to refer to another variable. In contrast, a pointer can be reassigned to point to different variables.
- A reference must be initialized when it is declared, whereas a pointer can be declared without initialization.
- A reference cannot be null, whereas a pointer can be null.
Using references in functions
One common use of references in C++ is to pass arguments to functions. When a reference is used as a function parameter, any changes made to the reference within the function are also made to the original variable.
Here’s an example:
#include <iostream> using namespace std; void increment(int& x) { x++; } int main() { int y = 10; cout << "Before increment: y = " << y << endl; increment(y); cout << "After increment: y = " << y << endl; return 0; }
In this example, we define a function increment that takes an integer reference as a parameter and increments the value of the reference.
We then call the increment function with an integer variable y as an argument.
Conclusion
In this tutorial, we’ve covered the basics of C++ references, including their syntax and examples of their usage.
We’ve also discussed the differences between references and pointers, as well as how to use references in functions.