The concept of 'const' is useful to indicate that an object's internal state should not be modifiable.
void f(const Object& o)
{
// 'o' cannot be modified
}
The ability to mark a non-static member as const
allows you to enforce that holders of a const
reference to the object cannot modify the internal state. For example, in the following scenario, Object
has an accessor str
that returns a reference to a internal state, which has both non-const and const overloads:
struct Object
{
// non-const: caller can modify internal state
std::string& str();
// const: caller cannot modify internal state
const std::string& str() const;
};
The non-const overload allows modification of internal state, but can be called only on a non-const reference to the object; the const overload does not allow modification of internal state and can be used with both const and non-const object references.
In the scenario you present, Game
appears to be a monolithic singleton object that contains everything in your program. If so, it seems questionable that it would be useful to ever pass a reference to a Game
- or to an object derived from it - making the distinction between const Game&
and Game&
somewhat moot. If this is the case, const-correctness has no utility and you will save yourself a headache if you simply make all members of Game
non-const.