Why can const int& bind to an int?

后端 未结 8 551
野性不改
野性不改 2021-02-04 04:39

In the C++ primer, I found that const int & can bind with a int object.I don\'t understand that,because I think const int & should bind with a

8条回答
  •  我在风中等你
    2021-02-04 04:47

    The value of the variable a which is 0 initially is holded at memory.

    If you write later: a=5, it'll set that value in memory to 5.

    When you write a reference const int & r = a; you are only saying that r should access that same place in memory where 5 is being hold.

    Because a is not const it can modify the value.

    This is basically how it works.

提交回复
热议问题