C + + keyword mutable further parsing

  • 2020-04-02 01:44:42
  • OfStack

Mutable in c + + keywords

Mutalbe means "variable" in Chinese and is the opposite of constant (const in C++).

In c + +, mutable is also set to breakthrough the limitation of const. Is decorated mutable variables that will forever is in a state of a variable, even in a const function.

As we know, an important function decorated by the const keyword is to protect the member variables in the class. That is, the function can use all the member variables in the class, but cannot change their values. However, in some special cases, we still need to modify some member variables of the class in the const function, because the member variables to be modified have little to do with the class itself, and even if they are modified, they will not have much impact on the class. Of course, you can say that you can remove the const keyword from the function. The problem is, I only want to modify one of the member variables, and the rest of the member variables still want to be protected by const.

The classic scenario is: I'm going to test the number of times a method is called.


class Person {
public:
 Person();
 ~Person();

 int getAge() const; 
 int getCallingTimes() const; 
private:
 int age;
 char *name;
 float score;
 int m_nums;  
};

Is the most common practice in getAge () method to add + 1 m_nums this variable in the body, but getAge () method is const, cannot be modified m_nums this variable, I don't want to get rid of the const keyword let others can modify member variables such as age, when the mutable keyword will come in handy:


#include <iostream>

class Person {
public:
 Person();
 ~Person();

 int getAge() const; 
 int getCallingTimes() const; 
private:
 int age;
 char *name;
 float score;
 mutable int m_nums;  
};

Person::Person()
{
 m_nums = 0;
}

Person::~Person(){}

int Person::getAge() const
{
 std::cout << "Calling the method" << std::endl;
 m_nums++;
 //Age = 4; The member variable still cannot be modified
 return age;
}

int Person::getCallingTimes()const
{
 return m_nums;
}

int main()
{
 Person *person = new Person();
 for (int i = 0; i < 10; i++) {
 person->getAge();
 }
 std::cout << "getAge() The method is called " << person->getCallingTimes() << " time " << std::endl;
 delete person;

 getchar();
 return 0;
}

Operation results:

Calling the method
Calling the method
Calling the method
Calling the method
Calling the method
Calling the method
Calling the method
Calling the method
Calling the method
Calling the method

The getAge() method is called 10 times

In this way, we can not only protect other member variables, but also make the value of the counter to accumulate.

It is important to note: mutable modify a const and the static type of the variable.

1, about the mutable keywords

Say first usage, mutable keyword can only modify the static and very member variables, using the mutable modified member variables in a const function values can be modified.

For example, the following code:


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 ;
}

Compile time complains, because the const member function to modify the member variables, but if statement m_nAccess add keyword mutable.

PS: The state of an object is determined by its non-static data members, so as the data members change, so does the state of the object! If a class member function is declared const type, said this function does not change the state of an object, which is the function does not modify the class non-static data members. But sometimes need to be in the class function of the data members of class assignment. This time you need to use mutable keyword.


Related articles: