TheMotleyBrit Posted August 19, 2009 Report Share Posted August 19, 2009 mutable is a little known C++ keyword used in the declaration of variables. According to http://www.cppreference.com/wiki/keywords/mutable ?The mutable keyword overrides any enclosing const statement. A mutable member of a const object can be modified.? This means that, instead of double pie; the variable pie can be declared as mutable double pie The difference between the mutabled and non-mutabled versions? Constant member functions can modify the mutabled one. #include <iostream> class Hello { public: Hello() { _pv_nValue = 0; } const int GetValue() const { return ++_pv_nValue; } private: mutable int _pv_nValue; }; void Do(const Hello& all) { std::cout << all.GetValue() << std::endl; std::cout << all.GetValue() << std::endl; std::cout << all.GetValue() << std::endl; } int main(const int nArgCount, const char** pcArgs) { Hello world; Do(world); return 0; } This will output: 1 2 3 ?But Jason,? you ask, ?Surely the whole point of constant member functions is to prevent the function altering the variables of the object.? Indeed, you are correct...in theory. However, as many people know (like 99% of all physicists), theory and practice are two separate things. Warning: Do NOT assume this means you can use mutable to weasel your way out of tricky situations and corners you coded yourself into...you can't. Well, you can but it's terrible practice and if you do it then more fool you. Think before you leap/code/whatever. ?So what purposes can mutables serve? you ask. Well, they are several possible reasonable purposes, but most if not all of them you'll never encounter in all your time programming. Ever. This would be why the mutable keyword is little-known about, it's little-used. One possible use is that perhaps, just perhaps, you wish to store how often a constant expression is called. What can you do? It's constant so can't modify a regular variable, so you're stuck here, right? In the words of Lex Luthor: WRONG! This is one of those situations mutable actually can be useful. As was illustrated above, you can use it to count the number of calls to a function. Another use is practically given to you in the name. Mutexes. Sometimes constant functions may still need to lock down the thread. Perhaps it needs to do some calculations with the variables of the function and doesn't need the horror of the variables potentially changing mid-way through. There are other potential, if unlikely, uses, but I'll leave thinking of or discovering those as an exercise for yourselves. I vaguely remember seeing mutable used at one or two points in the Loki library. Quote Link to comment Share on other sites More sharing options...
Crim Posted August 20, 2009 Report Share Posted August 20, 2009 coolio info.. btw, how come if you dont mind me asking, why you use std:: when instead you could just use, using namespace std which goes on a line below #include but before the class. Quote Link to comment Share on other sites More sharing options...
TheMotleyBrit Posted August 20, 2009 Author Report Share Posted August 20, 2009 Using std:: has just become a habit of mine. When working on larger projects it's best to avoid the "using" command, since it defeats the entire point of namespaces. For me this has carried over into examples I give as well :D Quote Link to comment Share on other sites More sharing options...
Crim Posted August 20, 2009 Report Share Posted August 20, 2009 ah ok, that makes sense.. i guess i just use it because im lazy and see it as an easy way out :D Quote Link to comment Share on other sites More sharing options...
MG-Zero Posted August 20, 2009 Report Share Posted August 20, 2009 Mutable isn't a little known keyword >_> It was one of the earliest things I learned in C++ OOP. Quote Link to comment Share on other sites More sharing options...
TheMotleyBrit Posted August 20, 2009 Author Report Share Posted August 20, 2009 As far as C++ keywords go it's pretty unmentioned, well at least I've barely ever seen it mentioned...which seems like it would be probably for the best, it's too abusable :D It's like private inheritance in that respect :P Quote Link to comment Share on other sites More sharing options...
hawthorneluke Posted August 21, 2009 Report Share Posted August 21, 2009 I've never heard of it lol. And nice to see you here, MG-Zero :D Quote Link to comment Share on other sites More sharing options...
Crim Posted August 21, 2009 Report Share Posted August 21, 2009 ive heard small bits of it, but other than that, it was unknown pretty much to me also. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.