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
const
keyword specifies that changes are prohibited (i.e., we have read-only access to the data).- The
const
keyword 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 haveconst
names that cannot be used to change it and non-const
names that can. - You cannot initialize a non-
const
reference (e.g., of typeint&
) with aconst
name (e.g., of typeconst int
). That would circumvent theconst
restriction! - 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.)