41

According to MSDN: "When following a member function's parameter list, the const keyword specifies that the function does not modify the object for which it is invoked."

Could someone clarify this a bit? Does it mean that the function cannot modify any of the object's members?

 bool AnalogClockPlugin::isInitialized() const
 {
     return initialized;
 }
Scott
  • 5,135
  • 12
  • 57
  • 74
  • [When its after the declaration its referring to the class 'this'](http://stackoverflow.com/questions/10982628/non-member-function-cannot-have-cv-qualifier) – Michael T Dec 03 '16 at 11:35

4 Answers4

43

It means that the method do not modify member variables (except for the members declared as mutable), so it can be called on constant instances of the class.

class A
{
public:
    int foo() { return 42; }
    int bar() const { return 42; }
};

void test(const A& a)
{
    // Will fail
    a.foo();

    // Will work
    a.bar();
}
Pierre Bourdon
  • 10,521
  • 4
  • 33
  • 27
  • 1
    You'll want to read up about C++ const-ness. You'll need to use const_iterator's on const STL objects, etc. – nall Oct 11 '09 at 04:55
  • 1
    Indeed, const-correctness is very important to understand and practice. – GManNickG Oct 11 '09 at 04:59
  • Also, const methods can only call static or other const methods. – Steve Guidi Oct 11 '09 at 05:50
  • Minor correction: The method _promises_ to not to change the object. (There's syntactic constructs allowing you to circumvent this promise. Of course, using them usually is gross - but they're there and there is a reason for that.) – sbi Oct 11 '09 at 06:56
  • 1
    Why does the first call fail? – BlackBear Mar 28 '12 at 16:45
  • When you add the const keyword to a method the this pointer will essentially become const, and you can therefore not change any member data. http://stackoverflow.com/questions/751681/meaning-of-const-last-in-a-c-method-declaration – camino Jan 22 '16 at 18:15
18

Note also, that while the member function cannot modify member variables not marked as mutable, if the member variables are pointers, the member function may not be able to modify the pointer value (i.e. the address to which the pointer points to), but it can modify what the pointer points to (the actual memory region).

So for example:

class C
{
public:
    void member() const
    {
        p = 0; // This is not allowed; you are modifying the member variable

        // This is allowed; the member variable is still the same, but what it points to is different (and can be changed)
        *p = 0;
    }

private:
    int *p;
};
blwy10
  • 4,862
  • 2
  • 24
  • 23
  • 1
    Good point. It's the difference between const-pointer-to-int, pointer-to-const-int and const-pointer-to-const-int. const methods make the pointer const. – UncleBens Oct 11 '09 at 11:06
  • You probably mean "...member variables _not_ marked as mutable...". – sbi Oct 11 '09 at 12:36
  • Oops, haha. Sorry about that. – blwy10 Oct 11 '09 at 13:26
  • If I want to define a method that do not modify the value of the member and the value of the member points to , should I do `void member() const const*` ? – Charles Chow Sep 18 '14 at 18:18
2

The compiler won’t allow a const member function to change *this or to invoke a non-const member function for this object

Satbir
  • 6,358
  • 6
  • 37
  • 52
2

As answered by @delroth it means that the member function doesn't modify any memeber variable except those declared as mutable. You can see a good FAQ about const correctness in C++ here

Naveen
  • 74,600
  • 47
  • 176
  • 233