title | ms.custom | ms.date | ms.technology | ms.topic | f1_keywords | dev_langs | helpviewer_keywords | ms.assetid | author | ms.author | ms.workload | |||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Mutable Data Members (C++) | Microsoft Docs |
11/04/2016 |
|
language-reference |
|
|
|
ebe89746-3d36-43a8-8d69-f426af23f551 |
mikeblome |
mblome |
|
This 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.
mutable member-variable-declaration;
For example, the following code will compile without error because m_accessCount
has been declared to be mutable, and therefore can be modified by GetFlag
even though GetFlag
is a const member function.
// mutable.cpp
class X
{
public:
bool GetFlag() const
{
m_accessCount++;
return m_flag;
}
private:
bool m_flag;
mutable int m_accessCount;
};
int main()
{
}