tags:

views:

130

answers:

3

hello, what is the difference between these two terms, and why do I need mutable, thanks in advance for any help

+8  A: 

You need mutable when there are fields in an object that can be considered "internal", i.e. any external user of the class cannot determine the value of these fields. The class might need to write to these fields even though the instance is considered constant, i.e. not changing.

Consider a hard drive; its cache is an example of such state. The cache is written to when data is read from the actual disk.

This is not possible to express in C++ without making the corresponding members mutable, to allow them to be changed even in methods marked const.

unwind
Another such example would be a mutex in an object shared by different threads. A read operation does not modify the object, but the mutex must be acquired and released in the process. The mutex member object clearly changes during the operation, even if the result of the operation does not change the object per-se. An operation is const on an object if the perceived state remains unmodified, even if internal elements change.
David Rodríguez - dribeas
@unwind: can You please give me small example(code) which shows what things I can do with mutable and can't with const, which also shows difference between logic and physical const'ness I found some explanations in google but can't find good example, with cache a little bit difficult to understand
rookie
+2  A: 

"Physical" constness comes from declaring an object const, and can be enforced by placing the object in read-only memory, so it cannot change. Attempting to change it will cause undefined behaviour; it might change, or it might not, or it might trigger a protection fault, or it might melt the memory chip.

"Logical" constness comes from declaring a reference or pointer const, and is enforced by the compiler. The object itself may or may not be "physically" const, but the reference cannot be used to modify it without a cast. If the object is not "physically" const, then C++ allows you to modify it, using const_cast to circumvent the protection.

A mutable class member can be modified even if the class itself (or the reference or pointer used to access it) is const. Examples of good uses of this are a mutex that must be locked during a read operation, and a cache to store the result of an expensive read operation. In both cases, the operation itself should be a const function (since it doesn't affect the visible state of the object), but it needs to modify the mutex or the cache, so these need to be mutable. It can also be abused to make the object visibly change when logically it shouldn't, so use it with care; only declare members mutable if they don't form part of the externally visible state.

Mike Seymour
+1, very nice explanation.
Dan
A: 

Scott Meyers, Effective C++, Item 3:

Use const whenever possible

has an excellent discussion (with examples) on this topic. Its hard to write better than Scott!

Note also that physical-constness is also known as bitwise-constness.

ArunSaha