C++ References
A reference variable is an alias, that is, another name for an already existing variable.
Once a reference is initialized to a variable, you can use either the reference name or the variable name to refer to the variable.
A reference must be initialized when it is defined, and once bound to a variable, it cannot be bound to another variable.
The syntax of a reference is as follows:
int a = 10;
int &ref = a; // ref 是 a 的引用
-
int &refindicatesrefis ainttype reference. -
refIsa's alias; operations onrefare directly applied toa.
C++ References vs Pointers
References are easily confused with pointers; there are three main differences between them:
- There is no null reference; a reference must be connected to a block of valid memory.
- Once a reference is initialized to an object, it cannot be made to point to another object. A pointer can point to another object at any time.
- A reference must be initialized when it is created. A pointer can be initialized at any time.
- The object of a reference must be a variable, whereas a pointer must be an address.
| Feature | reference | pointer |
|---|---|---|
| Definition and Initialization | must be initialized and cannot be null. |
can be uninitialized, can point to other objects in subsequent code, and can be null. |
| Syntax | Use & declaration, for example:int &ref = a; |
Use * declaration, for example:int *ptr = &a; |
| Rebinding | cannot be rebound; once initialized, it always refers to the same object. | can be made to point to other objects again, for example:ptr = &b; |
| Nullability | cannot be null; it must be bound to a valid object. |
can be null, indicating it does not point to any object. |
| Memory Usage | Does not occupy extra memory (the compiler usually optimizes it to directly operate on the referenced object). | Occupies extra memory (stores an address, usually one machine word, such as 4 or 8 bytes). |
| Access Method | Used directly without a dereference operator, for example:ref = 10; |
Requires the use of * dereference operator to access or modify the pointed-to object, for example:*ptr = 10; |
| Multi-level Indirect Access | Does not support multi-level indirect access (there cannot be a reference to a reference). | Supports multi-level indirect access (such as a pointer to a pointer:int **pptr;). |
| Function Parameter Passing | Commonly used for function parameter passing, with concise syntax, for example:void func(int &x) { x = 10; } |
can also be used for function parameter passing, but requires the use of a dereference operator, for example:void func(int *x) { *x = 10; } |
| Arrays and References | You cannot directly create an array of references, but you can create a reference to an array, for example:int (&ref)[10] = arr; |
You can create an array of pointers, and you can also create a pointer to an array, for example:int *ptrArr[10]; |
| Safety | is safer, cannot be null, and the syntax is more intuitive. |
is more flexible but error-prone (such as null pointers, wild pointers, etc.). |
| Underlying Implementation | Usually implemented through pointers, but the compiler optimizes it to directly operate on the referenced object. | Directly stores the memory address of the target object. |
Creating References in C++
Think of a variable name as a label attached to a variable's memory location; you can think of a reference as a second label attached to the variable's memory location. Therefore, you can access the variable's contents through either the original variable name or the reference. For example:
int i = 17;
We can declare a reference variable for i, as shown below:
int& r = i;
double& s = d;
In these declarations, & is read asreference.
Therefore, the first declaration can be read as "r is an integer reference initialized to i", and the second declaration can be read as "s is a double reference initialized to d".
The example below uses int and double references:
Example
#include
<
iostream
using
namespace
std;
int
main
(
)
{
// Declare simple variables
int
i
;
double
d
;
// Declare reference variables
int
&
r
=
i
;
double
&
s
=
d
;
i
=
5
;
cout
<<
"
Value of i :
"
<<
i
<<
endl
;
cout
<<
"
Value of i reference :
"
<<
r
<<
endl
;
d
=
11
.7
;
cout
<<
"
Value of d :
"
<<
d
<<
endl
;
cout
<<
"
Value of d reference :
"
<<
s
<<
endl
;
return
0
;
}
When the code above is compiled and executed, it produces the following result:
Value of i : 5
Value of i reference : 5
Value of d : 11.7
Value of d reference : 11.7
References are commonly used in function parameter lists and function return values. The following lists two important concepts related to C++ references that C++ programmers must be clear about:
| Concept | Description |
|---|---|
| Passing References as Parameters | C++ supports passing references as parameters to functions, which is safer than passing ordinary parameters. |
| Returning References as Return Values | You can return a reference from a C++ function, just like returning other data types. |