in C++: Is const reference means “read-only view of” or it requires immutability of object being referenced?

前端 未结 3 1339
青春惊慌失措
青春惊慌失措 2021-01-19 01:21

The question can be formulated by example as following: is this snippet of code valid?

int a = 1;
const int& ca = a;
++a; //< Q: Is this valid?
cout &         


        
3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-19 01:59

    It is valid code, because a is not const. A const reference means you cannot modify the refered-to object via the reference.

提交回复
热议问题