What can a 'const' method change?

后端 未结 5 632
遇见更好的自我
遇见更好的自我 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-06 00:12

    Most succinctly, it means that the type of this is const T * inside const member functions, where T is your class, while in unqualified functions it is T *.

    Your method set does not change data, so it can be qualified as const. In other words, myclass::data is accessed as this->data and is of type int * const.

提交回复
热议问题