CS 70

Key Points

  • A variable with a reference type (e.g., int&) is just another name for an existing variable (in this case an int).
  • 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 have const names that cannot be used to change it and non-const names that can.
    • You cannot initialize a non-const reference (e.g., of type int&) with a const name (e.g., of type const int). That would circumvent the const restriction!
    • A const-reference function parameter is a good way to avoid needless copying but still promise not to change the given argument.

(When logged in, completion status appears here.)