Search

6.11a — References and const

Reference to const value

Just like it’s possible to declare a pointer to a const value, it’s also possible to declare a reference to a const value. This is done by declaring a reference using the const keyword.

References to const values are often called “const references” for short.

Initializing references to const values

Unlike references to non-const values, which can only be initialized with non-const l-values, references to const values can be initialized with non-const l-value, const l-values, and r-values.

Much like a pointer to a const value, a reference to a const value can reference a non-const variable. When accessed through a reference to a const value, the value is considered const even if the original variable is not:

A reference to a const is often called a const reference for short, though this does make for some inconsistent nomenclature with pointers.

References to r-values extend the lifetime of the referenced value

Normally r-values have expression scope, meaning the values are destroyed at the end of the expression in which they are created.

However, when a reference to a const value is initialized with an r-value, the lifetime of the r-value is extended to match the lifetime of the reference.

Const references as function parameters

References used as function parameters can also be const. This allows us to access the argument without making a copy of it, while guaranteeing that the function will not change the value being referenced.

References to const values are particularly useful as function parameters because of their versatility. A const reference parameter allows you to pass in a non-const l-value argument, a const l-value argument, a literal, or the result of an expression:

The above prints

1234

To avoid making unnecessary, potentially expensive copies, variables that are not pointers or fundamental data types (int, double, etc…) should be generally passed by (const) reference. Fundamental data types should be passed by value, unless the function needs to change them.

Rule: Pass non-pointer, non-fundamental data type variables by (const) reference.

6.12 -- Member selection with pointers and references
Index
6.11 -- Reference variables

6 comments to 6.11a — References and const

  • Orfeas

    Hello Alex,
    Thanks for the wonderful tutorials! I came across these two short programs yesterday but I’m not entirely sure how they work.

    This prints:

    2 5 2
    5 5 5

    Why does the value of variable i change when I point pointer i to variable j’s memory adress?

    The next one is rather similar:

    This also prints:

    2 5 2
    5 5 5

    I assume &i is a reference, so does that mean that it doesn’t need to be initialized when part of a struct? and then I can’t even begin to explain what happens on starting from a.i = j.

    Could you help me understand how they work?

    Thanks in advance!

  • Khang

    Hi, Alex.
    At "References to r-values extent the life time of the referenced value"
    What is the difference between
    [const int &ref=2+3;]
    and
    [const int value=2+3;].
    It seems that they’re all extend the life time of "2+3", so is it too obvious
    to say the statement above?

  • FinalDevil

    Hi Alex, at the command

    , should it be

    ? It is reference to rvalue, so we use double ampersand?

    • Alex

      There’s no need to double-ampersand here. A double-ampersanded reference variable is still treated as an l-value (after all, it has an address). So essentially, in such a case, the second ampersand is ignored.

Leave a Comment

Put C++ code inside [code][/code] tags to use the syntax highlighter