Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: [C++] mutable keyword  (Read 883 times)

0 Members and 1 Guest are viewing this topic.
[C++] mutable keyword
« on: July 16, 2009, 05:53:37 pm »
  • (y)(;>.<;)(y)
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3293
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
Code: [Select]
double pie;
the variable pie can be declared as
Code: [Select]
mutable double pie
The difference between the mutabled and non-mutabled versions? Constant member functions can modify the mutabled one.

Code: [Select]
#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.
« Last Edit: July 17, 2009, 11:35:13 am by TheDarkJay »
Logged
Pages: [1]   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.034 seconds with 36 queries.