Monday, August 17, 2009

c++: mutable--> used only for non-static and non-const data members

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.

SEE FOLLOWING CODE :-

********************************************

class Mutable
{
private :
int m_iNonMutVar;
mutable int m_iMutVar;

public:
Mutable();
void TryChange() const;

};

Mutable::Mutable():m_iNonMutVar(10) m_iMutVar(20) {};

void Mutable::TryChange() const
{
m_iNonMutVar 100; // THis will give ERROR
m_iMutVar 200; // This will WORK coz it is mutable
}

No comments:

Post a Comment