What are the semantics of a const member function?

后端 未结 9 764
囚心锁ツ
囚心锁ツ 2020-12-06 16:51

I understand that the function is not allowed to change the state of the object, but I thought I read somewhere that the compiler was allowed to assume that if the function

9条回答
  •  暖寄归人
    2020-12-06 17:44

    No.

    A const method is a method that doesn't change the state of the object (i.e. its fields), but you can't assume that given the same input, return value of a const method is determined. In other words, const keyword does NOT imply that the function is one-to-one. For instance a method that returns the current time is a const method but its return value changes between calls.

提交回复
热议问题