<p>Given a declaration like this:</p>

<pre><code>class A {
public:
    void Foo() const;
};
</code></pre>

<p>What does it mean?</p>

<p>Google turns up this:</p>

<blockquote>
  <p>Member functions should be declared with the const keyword after them if they can operate on a const (this) object. If the function is not declared const, in can not be applied to a const object, and the compiler will give an error message.</p>
</blockquote>

<p>But I find that somewhat confusing; can anyone out there put it in better terms?</p>

<p>Thanks. </p>

[{'answer_id': 49036, 'author': 'John Boker', 'author_id': 2847, 'author_profile': 'https://Stackoverflow.com/users/2847', 'pm_score': 1, 'selected': False, 'text': '<p>that will cause the method to not be able to alter any member variables of the object</p>\n'}, {'answer_id': 49039, 'author': '1800 INFORMATION', 'author_id': 3146, 'author_profile': 'https://Stackoverflow.com/users/3146', 'pm_score': 2, 'selected': False, 'text': '<p>Functions with <code>const</code> qualifier are not allowed to modify any member variables. For example:</p>\n\n<pre><code>class A\n{\n    int x;\n    mutable int y;\n\n    void f() const\n    {\n      x = 1; // error\n      y = 1; // ok because y is mutable\n    }\n};\n</code></pre>\n'}, {'answer_id': 49044, 'author': 'dfjacobs', 'author_id': 4848, 'author_profile': 'https://Stackoverflow.com/users/4848', 'pm_score': 2, 'selected': False, 'text': '<p>C++ objects can be declared to be const:</p>\n\n<pre><code>const A obj = new A();\n</code></pre>\n\n<p>When an object is const, the only member functions that can be called on that object are functions declared to be const.  Making an object const can be interpreted as making the object readonly.  A const object cannot be changed, i.e. no data members of the object can be changed.  Declaring a member function const means that the function is not allowed to make any changes to the data members of the object.</p>\n'}, {'answer_id': 49056, 'author': 'Henk', 'author_id': 4613, 'author_profile': 'https://Stackoverflow.com/users/4613', 'pm_score': 5, 'selected': True, 'text': "<p>Consider a variation of your class <code>A</code>.</p>\n\n<pre><code>class A {\npublic:\n    void Foo() const;\n    void Moo();\n\nprivate:\n    int m_nState; // Could add mutable keyword if desired\n    int GetState() const   { return m_nState; }\n    void SetState(int val) { m_nState = val; }\n};\n\nconst A *A1 = new A();\nA *A2 = new A();\n\nA1-&gt;Foo(); // OK\nA2-&gt;Foo(); // OK\n\nA1-&gt;Moo(); // Error - Not allowed to call non-const function on const object instance\nA2-&gt;Moo(); // OK\n</code></pre>\n\n<p>The <code>const</code> keyword on a function declaration indicates to the compiler that the function is contractually obligated not to modify the state of <code>A</code>. Thus you are unable to call non-<code>const</code> functions within <code>A::Foo</code> nor change the value of member variables.</p>\n\n<p>To illustrate, <em>Foo()</em> may not invoke <code>A::SetState</code> as it is declared non-<code>const</code>, <code>A::GetState</code> however is ok because it is explicitly declared <code>const</code>. The member <code>m_nState</code> may not be changed either unless declared with the keyword <code>mutable</code>.</p>\n\n<p>One example of this usage of <code>const</code> is for 'getter' functions to obtain the value of member variables.</p>\n\n<blockquote>\n  <p>@1800 Information: I forgot about mutable!</p>\n</blockquote>\n\n<p>The <code>mutable</code> keyword instructs the compiler to accept modifications to the member variable which would otherwise cause a compiler error. It is used when the function needs to modify state but the object is considered logically consistent (constant) regardless of the modification.</p>\n"}, {'answer_id': 49076, 'author': 'Pat Notz', 'author_id': 825, 'author_profile': 'https://Stackoverflow.com/users/825', 'pm_score': 3, 'selected': False, 'text': "<p>This is not an answer, just a side comment.  It is <strong>highly</strong> recommended to declare variables and constants <code>const</code> as much as possible.  </p>\n\n<ol>\n<li>This communicates your intent to users of your class (even/especially yourself).</li>\n<li>The compiler will keep you honest to those intentions. -- i.e., it's like compiler checked documentation.</li>\n<li>By definition, this prevents state changes you weren't expecting and can, possibly, allow you to make reasonable assumptions while in your methods.</li>\n<li><code>const</code> has a funny way of propagating through your code.  Thus, it's a really good idea to start using <code>const</code> as early and as often as possible.  Deciding to start <code>const</code>-ifying your code late in the game can be painful (easy, but annoying).</li>\n</ol>\n\n<p>If you're using a language with static, compile time checks it's a great idea to make as much use of them as possible... it's just another kind of testing really.</p>\n"}, {'answer_id': 58843, 'author': 'Tyler', 'author_id': 3561, 'author_profile': 'https://Stackoverflow.com/users/3561', 'pm_score': 2, 'selected': False, 'text': '<p>Two suggested best practices from experience:</p>\n\n<p>(1) <strong>Declare const functions whenever possible</strong>.  At first, I found this to be just extra work, but then I started passing my objects to functions with signatures like f(const Object&amp; o), and suddenly the compiler barfed on a line in f such as o.GetAValue(), because I hadn\'t marked GetAValue as a const function.  This can surprise you especially when you subclass something and don\'t mark your version of the virtual methods as const - in that case the compile could fail on some function you\'ve never heard of before that was written for the base class.</p>\n\n<p>(2) <strong>Avoid mutable variables</strong> when it\'s practical.  A tempting trap can be to allow read operations to alter state, such as if you\'re building a "smart" object that does lazy or asynchronous i/o operations.  If you can manage this with only one small mutable variable (like a bool), then, in my experience, this makes sense.  However, if you find yourself marking every member variable as mutable in order to keep some operations const, you\'re defeating the purpose of the const keyword.  What can go wrong is that a function which thinks it\'s not altering your class (since it only calls const methods) my invoke a bug in your code, and it could take a lot of effort to even realize this bug is in your class, since the other coder (rightly) assumes your data is const because he or she is only calling const methods.</p>\n'}, {'answer_id': 60123, 'author': 'MP24', 'author_id': 6206, 'author_profile': 'https://Stackoverflow.com/users/6206', 'pm_score': 2, 'selected': False, 'text': "<blockquote>\n  <p>const has a funny way of propagating through your code. Thus, it's a really good idea to start using const as early and as often as possible. Deciding to start const-ifying your code late in the game can be painful (easy, but annoying).</p>\n</blockquote>\n\n<p>Additionally, you will easily run into problems if methods that should be const aren't! This will creep through the code as well, and make it worse and worse.</p>\n"}]
['https://Stackoverflow.com/questions/49035', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/61/']