c++ - If QList instance is const, does it mean each element is constant? -
the following code fails compile:
void func(const qlist<int>& list){ auto& elem = list[0]; } the problem cannot bind const element non-const reference. following code works:
const auto& elem = list[0]; can explain why passing list const makes element const?
can explain why passing list
constmakes elementconst?
this semantics standard containers following. here's reason can see:
const int arr1 = {10, 20}; arr1[0] = 40; // error. elements of arr1 cannot modified. const std::vector<int> arr2 = {10, 20}; arr2[0] = 40; // same semantics. error. elements of arr2 cannot modified. extending logic qlist, elements of qlist const if qlist const.