1, about mutable keyword
First of all, the mutable keyword can only modify non-static and very variable member variables, and the values of the member variables decorated with mutable can be modified in the const function.
For example, the following code:
Copy Code code as follows:
Class Demo
{
Public:
Demo () {}
~demo () {}
Public:
BOOL Getflag () const
{
m_naccess++;
return m_bflag;
}
Private:
int m_naccess;
BOOL M_bflag;
};
int main ()
{
return 0;
}
The error occurs when compiling, because the const member function modifies the member variable, but it is OK to add the keyword mutable if the declaration is m_naccess.
PS: The state of an object is determined by the Non-static data member of the object, so the state of the image changes as the data member changes! If the member function of a class is declared as a const type, Indicates that the function does not change the state of the object, that is, the function does not modify the Non-static data members of the class. However, there are times when you need to assign values to the data members of a class in the class function. This time you need to use the mutable keyword.