Maxpm Maxpm - 4 months ago 19
C++ Question

Constant Member Functions

After reading this, it is my understanding that declaring a method as

const
prevents it from accidentally modifying the class's member variables.


  • Are
    const
    methods commonly used?

  • Should they be used for everything that shouldn't modify member variables?


Answer

Yes, const should always be used, when appropriate.

It lets your compiler check your application logic, statically asserting const-correctness for free!

Some people even say that const should be the default, and you should be forced to use mutable for what is non-constant.