Back to the High Programmer's Blog
Fscking spammers. You're wasting your time. 1: I use rel=nofollow, so you're not getting any Google PageRank for your spam. 2: I flag spam pretty quickly; I'm notified of new comments within a few minutes.
All that said, it's getting a little annoying flagging the spam, so I've added the World's Dumbest CAPTCHA. Inspired by Lawrence Lessig's blog, you just type the magic word (it's "human", for now), into the provided box. It's not great security, but hopefully it's enough. On the up side, it's really easy to implement and really easy to use (I hope. Sorry for the bother, blame the scum willing to piss all over the internet for short term gain.
If instead of the word "human", the word is random, and you don't tell us what the word is, I bet that'll cut down on the rest of the content free comments that might get posted.
Excellent article. Just want to send some thanks for clearing that up. (sorry to hear about the spam, sucks)

Good Article. Thanks for clarifying.

Very helpful article: I just spend 3 days entangled in a debugging issue related to mutable and you have put in words what I had been sensing all the way. Thanks a lot!

Thank you for making this subject clear by presenting clean examples. =)
Thanks for clarifying 'mutable.' It's nice to know that I continue to never need it. That C++ needs keywords like 'mutable' (like 'virtual') is evidence that C++ is a terrible language.
"That C++ needs keywords like 'mutable' (like 'virtual') is evidence that C++ is a terrible language."
Utter nonsense. C++'s const and mutable are a powerful pair of features that allow greater enforcement of programming contracts. This post gives a good example of C++, thanks to const, being able to enforce a programming contract in a way that Java can't. (I've chosen Java because it's frequently regarded as being much better than C++. Admittedly I have no idea what languages you actually prefer over C++.) And once you have const, you'll need (however infrequently) mutable to handle the special cases where an object is logically constant, but for implementation reasons need to change.
Alan: I just read your posting, and these comments, and thought I'd add a pertinent comment. I often have to keep sets of data in memory, and typically package each "element of a set" (think struct !) as a class. Once its in a class its easy to store in a set; I just implement operator < so I know which members of the class form the "key" and which are associated data. This works nicely for me until I need to modify the class members which are not a part of the key (obviously I don't want to modify the key!). The compiler I use on our SUN machines here at work complain that I'm trying to modify a const object (the object member in the set). The VC++ compiler I ofter use for initial development doesn't complain about this. I can't decide which is worse: complaining about modifying a non-const member (the compiler can't tell which are key elements and which aren't !), or the lack of complaint (it will allow me to ruin the underlying b-tree structure by modifying a key-member? Sheesh!). I just battled through this this am, and ended up just putting the data into both sides of a map (I know! I know!). When I have a sec I'm going to explore "mutable" on the Sun side to see if it stops complaining. There's no hope for the VC++ side, as its allowing something it really shouldn't! Brooks

Nice article, I have seen the best use of mutable when dealing with classes designed to be thread-safe. Since many string and classes of the like (which are very mutable in C++ unlike Java and C#) don't handle being written to and read from at the same time very well, it is nice to have thread synchronization features of the OS to prevent corruption, problem is, without mutable... const functions can't manipulate these structures. To Andrei MR: Java does on the other hand have some point in making the class define the constness of an object rather than the methods, but it also limits the possibilities. Doesn't make either a better languages, just like C++ requires the nasty looking dynamic_cast when trying to reproduce even Java's one class multiple interfaces inheritance if both a base class and a implemented interface have the same base. Java can handle this with nicer looking code and probably better than C++.
Thanks for that article. I kept asking myself why someone would want the mutable keyword, since it just seemed like a bad idea. The pi example makes sense, although I still see very few places this keyword is a good practice.
Happy coding, A.A.Que
Shouldn't Employee::promote lack the const keyword?
Kenneth: You are most certainly correct. Thanks for pointing it out. I've fixed it.
Your article is very clarifying. I've put a reference to it in our website.
These articles may be of your interest: http://www.jquantlib.org/index.php/DesignType... http://www.jquantlib.org/index.php/Providing_...
Thanks a lot.
-- Richard

The article is good. This topic is also very well explainded into the B.Strustrup book.
For the people that are saying that mutable makes C++ a bad language: "That C++ needs keywords like 'mutable' (like 'virtual') is evidence that C++ is a terrible language.", I want to expalin them that this is the oposite.
Mutable makes C++ a better language. Let me explain that:
An object saves two type of information: 1.- The logical information. For instance in the Employ object, it stores all the information thas has to be with an employ (name, telephone, etc.) 2.- The internal funcional information. This is memory that is stored into the object to make that the object work well into the program. For instance gc: garabage collector pointers, the pi number (good example), counters for debuger and others. All of this information do not have realition with the employ abstract idea. It is used to make the house keeping of the program.
Well, when you instruct that and object is constant, normally you are trying to keep constant the abstract idea of what is an employ but you don't have any problem if the internal house keeping information is changed. Without this change the gc will not function for example, or you can reallocate the object in other place of memory and this needs to change some internal pointers, intrusive list pointers, etc.
This is were the mutable keywords take place. When a programmer reads that a variable is mutable, he knows that this variable is not part of the abtract representation of an employ. On the contrary, he knows that this variable is beeing used to sustaint that object Employ into the program and this information can change for internal reasons without altering the employ information.
mutable was not necesary in the past becasu you can cast a memory to be not constant and then modify it. However, C++ has the mutable keyword because this makes the intention of the programmer more clear for the reader informing he that this memory is used only for internal functionality only. Don have nothig to do with the employ.
This makes C++ a better language! Because you can write preciselly what variables are part of the employ information (abstract idea) and what variables are for internal function only (how the object it is hose keept).
Regards.