Skip to content

Latest commit

 

History

History
40 lines (32 loc) · 993 Bytes

mutable-data-members-cpp.md

File metadata and controls

40 lines (32 loc) · 993 Bytes
descriptiontitlems.datef1_keywordshelpviewer_keywords
Learn more about: Mutable Data Members (C++)
Mutable Data Members (C++)
03/14/2024
mutable_cpp
mutable keyword [C++]

Mutable Data Members (C++)

This keyword can only be applied to non-static, non-const, and non-reference 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.

Syntax

mutable member-variable-declaration; 

Remarks

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.cppclassX { public:boolGetFlag() const { m_accessCount++; return m_flag; } private:bool m_flag; mutableint m_accessCount; };

See also

Keywords

close