Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The concept of 'const' is useful to indicate that an object's internal state should not be modifiable.</p> <pre><code>void f(const Object&amp; o) { // 'o' cannot be modified } </code></pre> <p>The ability to mark a non-static member as <code>const</code> allows you to enforce that holders of a <code>const</code> reference to the object cannot modify the internal state. For example, in the following scenario, <code>Object</code> has an accessor <code>str</code> that returns a reference to a internal state, which has both non-const and const overloads:</p> <pre><code>struct Object { // non-const: caller can modify internal state std::string&amp; str(); // const: caller cannot modify internal state const std::string&amp; str() const; }; </code></pre> <p>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.</p> <p>In the scenario you present, <code>Game</code> appears to be a monolithic <a href="http://en.wikipedia.org/wiki/Singleton_pattern" rel="nofollow">singleton</a> object that contains everything in your program. If so, it seems questionable that it would be useful to ever pass a reference to a <code>Game</code> - or to an object derived from it - making the distinction between <code>const Game&amp;</code> and <code>Game&amp;</code> 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 <code>Game</code> non-const.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload