Preventing a const member function from changing a member array

前端 未结 3 485
时光取名叫无心
时光取名叫无心 2021-01-14 15:14

Apparently, a const member function is still allowed to change data that the class member are pointing to. Here\'s an example of what I mean:

class MyClass
{         


        
3条回答
  •  滥情空心
    2021-01-14 15:57

    There is no way to do what you want. Your const member function can't change the value of data (the address it points to), but there is nothing about not changing the contents it points to as you have already noticed. Using a std::vector would help, since within a const member function you would actually have a const vector, not being able to call any of its mutable functions.

提交回复
热议问题