C++ – Why does this const member function allow a member variable to be modified

c++constants

class String
{

    private:
        char* rep;

    public:
        String (const char*);
        void toUpper() const;
};


String :: String (const char* s)
{
    rep = new char [strlen(s)+1];
    strcpy (rep, s);
}


void String :: toUpper () const
{
    for (int i = 0; rep [i]; i++)
    rep[i] = toupper(rep[i]);
}


int main ()
{
    const String lower ("lower");
    lower.toUpper();

    cout << lower << endl;
    return 0;
}

Best Solution

A const member function, is a member function that does not mutate its member variables.

const on a member function does not imply const char *. Which would mean that you can't change the data in the address the pointer holds.

Your example does not mutate the member variables themselves.

A const on a member function, will ensure that you treat all of your member variables as const.

That means if you have:

int x;
char c;
char *p;

Then you will have:

const int x;
const char c;
char * const p; //<-- means you cannot change what p points to, but you can change the data p points to

There are 2 types of const pointers. A const member function uses the one I've listed above.


A way to get the error you want:

Try changing:

char * rep;

to:

char rep[1024];

And remove this line:

rep = new char [strlen(s)+1];

It will throw the error you are expecting (can't modify members because of const keyword)

Because there is only 1 type of const array. And that means you cannot modify any of its data.


Now the whole system is actually broken with the following example:

class String
{

    private:
        char rep2[1024];
        char* rep;

 ...


 String :: String (const char* s)
 {
    rep = rep2;
    strcpy (rep, s); 
 }

So the lesson to learn here is that the const keyword on member functions does not ensure that your object will not change at all.

It only ensures that each member variable will be treated as const. And for pointers, there is a big diff between const char * and char * const.

Most of the time a const member function will mean that the member function will not modify the object itself, but this is not always the case, as the above example shows.