c++optimizationconstantskeyword

How does const after a function optimize the program?


I've seen some methods like this:

void SomeClass::someMethod() const;

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

Edit

I see that the first part of this question has been asked before... BUT, it still doesn't answer the second part: how would this optimize the program?


Solution

  • 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.