Comments

Back to the High Programmer's Blog

Back to the article

Alan De Smet at 21:18 Nov 12, 2006

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.

Peter Keller at 22:31 Nov 14, 2006

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.

Paul at 12:36 Jan 12, 2007

Excellent article. Just want to send some thanks for clearing that up. (sorry to hear about the spam, sucks)

Ram at 2:40 May 18, 2007

Good Article. Thanks for clarifying.

Christian at 3:38 Aug 3, 2007

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!

Bob at 9:11 Aug 28, 2007

Thank you for making this subject clear by presenting clean examples. =)

Andrei MR at 10:32 Aug 29, 2007

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.

Alan De Smet at 17:44 Aug 29, 2007

"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.

Brooks Van Pelt at 5:06 Sep 20, 2007

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

Matthew at 7:20 Dec 4, 2007

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++.

Andrew Que at 9:55 Jan 23, 2008

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

Kenneth Porter at 16:10 Mar 7, 2008

Shouldn't Employee::promote lack the const keyword?

Alan De Smet at 17:22 Mar 7, 2008

Kenneth: You are most certainly correct. Thanks for pointing it out. I've fixed it.

Richard Gomes at 6:07 Jun 7, 2008

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

Mario Galindo at 16:19 Jul 4, 2008

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.

Post Message

Your name:
The word "human": (To fight comment spam you must enter the word "human" in this box.)
Your web site: (optional)
Your email address: (optional)
(Your email address is not displayed to others nor stored. It is only used to display a Gravatar Avatar.)



Supported tags: [B]Bold text[/B]   [I]Italic text[/I]   [TT]Typewriter font[/TT]   [CODE]Preformatted text[/CODE]   [QUOTE]Block quote[/QUOTE]   [QUOTE="Original Author"]Block quote[/QUOTE]   [URL="http://www.example.com"]Example Corporation[/URL]