How does const after a function optimize the program?

前端 未结 6 1902
星月不相逢
星月不相逢 2021-01-31 05:29

I\'ve seen some methods like this:

void SomeClass::someMethod() const;

What does this const declaration do, and how can it help optimize a prog

6条回答
  •  礼貌的吻别
    2021-01-31 06:00

    If the compiler knows that the fields of a class instance are not modified across a const member function call, it doesn't have to reload any fields that it may have kept in registers before the const function call.

    This is sort of referred to the in C++ FAQ in the discussion on const_cast.

提交回复
热议问题