Reference
:material-circle-edit-outline: 约 96 个字 :fontawesome-solid-code: 1 行代码
Declaring
Rules
- References must be initialized when defined
- Bindings don’t change at run time, unlike pointers
- The target of a reference must have a location!
- No references to references
- No pointers to references
int&* p; // illegal- Reference to pointer is ok
- No arrays of references
Pointers vs. References
| References | Pointers |
|---|---|
| can't be null | can be set to null |
| are dependent on an existing variable, they are an alias for an variable | pointer is independent of existing objects |
| can't change to a new "address" location | can change to point to a different address |