Key Points
- A variable with a reference type (e.g.,
int&) is just another name for an existing variable (in this case anint). - An important use of references is as function parameters:
- Allows the function to alter data outside of its stack frame.
- Prevents unnecessary copy operations.
- The
constkeyword specifies that changes are prohibited (i.e., we have read-only access to the data).- The
constkeyword affects what we are allowed to do via a particular name, not what others might be able to do via a different name, so the same piece of data can haveconstnames that cannot be used to change it and non-constnames that can. - You cannot initialize a non-
constreference (e.g., of typeint&) with aconstname (e.g., of typeconst int). That would circumvent theconstrestriction! - A
const-reference function parameter is a good way to avoid needless copying but still promise not to change the given argument.
- The
(When logged in, completion status appears here.)