What can a 'const' method change?

后端 未结 5 618
遇见更好的自我
遇见更好的自我 2020-12-05 23:46

C++ methods allow a const qualifier to indicate that the object is not changed by the method. But what does that mean? Eg. if the instance variables are pointer

5条回答
  •  攒了一身酷
    2020-12-05 23:53

    const when applied to a method means:

    This means that the state of the object will not be changed by the method.
    This means any members that are part of the objects state can not be modified, nor can any functions that are not also const be called.

    As this relates to pointers. It means the pointer (if it is part of the state) can not be changed. But the object the pointer points at is part of another object so that means you can call non cost methods on this object (as it is not part of the state of this object).

提交回复
热议问题