c++ references appear reassigned when documentation suggests otherwise

后端 未结 2 1806
孤独总比滥情好
孤独总比滥情好 2021-01-15 15:01

According to this question you can\'t change what a reference refers to. Likewise the C++ Primer 5th Edition states

Once we have defined a reference,

2条回答
  •  逝去的感伤
    2021-01-15 15:43

    You are not reassigning a reference. A reference acts as an alias for a variable. In this case, ref is an alias for a, so

    ref = b;
    

    is the equivalent of

    a = b;
    

    You can easily check that by printing out the value of a:

    std::cout << a << std::endl; // prints 4
    

提交回复
热议问题