6

I didn't find any topics related to mutable const on SO. I have reduced code to minimal working code (on visual studio). If we uncomment //*pdata = 11;, the compiler complains about const-ness. I wonder how mutable const works.

class A { public: void func(int & a) const { pdata = &a; //*pdata = 11; } mutable const int * pdata; }; int main() { const A obj; int a = 10; obj.func(a); } 
5
  • This code looks pretty much like nonsense... Commented Jul 20, 2017 at 3:53
  • I lack the ability to put more sense in an example. Commented Jul 20, 2017 at 3:59
  • This would be a bit more interesting an example. Note how we're modifying a member in a const object. Commented Jul 20, 2017 at 4:04
  • stuff makes a bit more sense if you never put "const" first on a type. instead, say "int const * pdata" then read it backwards, so it's clear it's "pdata is a pointer to a constant int". Then it makes sense that the pointer can be changed, not the const int. Commented Jul 20, 2017 at 4:25
  • @shainuvy Sorry, I meant not your example, but "mutable const" Commented Jul 20, 2017 at 5:55

3 Answers 3

13

This example is a little confusing, because the mutable keyword is not part of the type specifier const int *. It's parsed like a storage class like static, so the declaration:

mutable const int *pdata; 

says that pdata is a mutable pointer to a const int.

Since the pointer is mutable, it can be modified in a const method. The value it points to is const, and cannot be modified through that pointer.

Sign up to request clarification or add additional context in comments.

1 Comment

That's weird; I would have thought that would look like const int * mutable pdata; But I guess it makes sense
7

You are correct in understanding that a mutable const class member is meaningless. Your example is more demonstrating a quirk of how const works with pointers.

Consider the following class.

class A { const int * x; // x is non-const. *x is const. int const * y; // y is non-const. *y is const. int * const z; // z is const. *z is non-const. }; 

So const has different meanings depending on where you write it.

Since x and y are non-const, there's no contradiction in making them mutable.

class A { mutable const int * x; // OK mutable int const * y; // OK mutable int * const z; // Doesn't make sense }; 

Comments

3

mutable const sounds like an oxymoron, but it actually has a perfectly sensible explanation. const int * implies that the pointed-to integer value cannot be changed through that pointer. mutable means that the pointer itself can be changed to point to another int object, even if the A object to which the pdata member belongs it itself const. Again, the pointed to value can't be changed through that pointer, but that pointer itself can be reseated.

Your code fails when the assignment statement is uncommented because that assignment violates your promise not to modify the pointed to value (the const int * part).

2 Comments

I thought mutable just meant "non const".even when the object is const. sounds like you're talking about volatile instead of mutable. Matt's answer sounds more like what I was expecting.
Sorry...you're right. Brain fart. Editing now--stand by.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.