C++ Reference
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 the reference name or the variable name to refer to the variable.
A reference must be initialized at definition, and once bound to a variable, it cannot be bound to another variable.
The syntax for references is as follows:
int a = 10; int &ref = a; // ref 是 a 的引用
int &refRepresentsrefis aintReference to type.refYesaan alias for,refThe operation directly acts ona。
C++ References vs Pointers
References are easily confused with pointers; there are three main differences between them:
- There are no null references; a reference must connect to a piece of valid memory.
- Once a reference is initialized to an object, it cannot be pointed to another object. A pointer can point to another object at any time.
- A reference must be initialized when created. A pointer can be initialized at any time.
- The object referenced by a reference must be a variable, while a pointer must be an address.
| Features | Reference | Pointer |
|---|---|---|
| Definition and initialization | Must be initialized, and cannot benull。 | It can be left uninitialized, can point to other objects in subsequent code, and can benull。 |
| Syntax | Usage&Declaration, for example:int &ref = a; | Usage*Declaration, for example:int *ptr = &a; |
| rebinding | It cannot be rebound; once initialized, it always refers to the same object. | Can be reassigned to point to another object, for example:ptr = &b; |
| Null value (Nullability) | cannot benull, must be bound to a valid object. | can benull, meaning it points to no object. |
| Memory usage | It does not occupy extra memory (the compiler usually optimizes it to directly operate on the referenced object). | It occupies extra memory (stores an address, usually one machine word, e.g., 4 bytes or 8 bytes). |
| Access method | It is used directly, without needing a dereference operator, for example:ref = 10; | need to use*Use the dereference operator to access or modify the pointed-to object, for example:*ptr = 10; |
| Multi-level indirection | It does not support multiple levels of indirection (there can be no reference to a reference). | It supports multiple levels of indirection (e.g., pointer to pointer:int **pptr;)。 |
| Function parameter passing | It is often used for function parameter passing, with concise syntax, for example:void func(int &x) { x = 10; } | It can also be used for function parameter passing, but requires the 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]; |
| Security | Safer, cannot benull, and the syntax is more intuitive. | It is more flexible, but error-prone (e.g., null pointers, dangling pointers, etc.). |
| Underlying implementation | It is 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 the variable in its memory location. You can treat a reference as a second label attached to the variable in its 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 follows:
int& r = i; double& s = d;
In these declarations,&read asReference。
Therefore, the first declaration can be read as"r is an integer reference initialized to i", the second declaration can be read as"s is a double reference initialized to d"。
The following example uses int and double references:
Example
When the above code is compiled and executed, it produces the following results:
Value of i : 5 Value of i reference : 5 Value of d : 11.7 Value of d reference : 11.7
References are commonly used for function parameter lists and function return values. Listed below are two important concepts related to C++ references that C++ programmers must be clear about:
| Concepts | Description |
|---|---|
| Passing References as Parameters | C++ supports passing references to functions as parameters, which is safer than passing ordinary parameters. |
| Using references as return values | A reference can be returned from a C++ function, just like other data types. |