Tuesday, August 28, 2007

C++ keywords

mutable keyword:
----------------

The mutable keyword can only be applied to non-static and non-const data members of a class. If a data member is declared mutable, then it is legal to assign a value to this data member from a const member function.

class CMyClass
{
int a;
mutable int b;
public:

CMyClass(int x)
:a(x),b(x)
{}


int getValueA() const
{
a++; //Error! we cant modify a variable in
//const member function.
return a;
}

int getValueB() const
{
b++; //This is valid since the variable is declared as
//mutable
return b;
}

};