Why does “most important const” have to be const?

后端 未结 3 536
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-16 10:44

In http://herbsutter.com/2008/01/01/gotw-88-a-candidate-for-the-most-important-const/ it mentions \"most important const\" where by C++ deliberately specifies that binding a

3条回答
  •  悲哀的现实
    2020-12-16 11:13

    Consider the following:

    int& x = 5;
    x = 6;
    

    What should happen if this was allowed? By contrast, if you did

    const int& x = 5;
    

    there would be no legal way to modify x.

提交回复
热议问题