Mutable used to modify member variables in a const member function

Source: Internet
Author: User

http://no001.blog.51cto.com/1142339/389840/

The Chinese meaning of Mutalbe is "mutable, variable", and constant (both const in C + +) are antonyms.

In C + +, mutable is also set to break the limits of Const. Variables that are modified by mutable will always be in a mutable state, even in a const function.

We know that if the member function of a class does not change the state of the object, then this member function is generally declared as Const. However, there are times when we need to modify some data members that are unrelated to the class state in the Const function, then this data member should be mutalbe.

Here is a small example:

Class Clxtest
{
Public
void Output () const;
};

void Clxtest::output () const
{
cout << "Output for test!" << Endl;
}

void outputtest (const clxtest& LX)
{
Lx. Output ();
}
The member function output of the class clxtest is used for outputting, and does not modify the state of the class, so it is declared as Const.

Function outputtest is also used for output, which invokes the output method of the object LX, in order to prevent calls to other member functions in the function to modify any member variables, so the parameters are also const decorated.

If now, let's add a function: Calculate the number of outputs per object. If the variable used to count is a normal variable, then the value of the variable cannot be modified in the const member function output, and the variable is independent of the state of the object, so the const property of the output should be removed in order to modify the variable. At this point, it's time for our mutable to play-just use Mutalbe to modify the variable, and all the problems will be solved.

The following is the modified code:

Class Clxtest
{
Public
Clxtest ();
~clxtest ();

void Output () const;
int getoutputtimes () const;

Private
mutable int m_itimes;
};

Clxtest::clxtest ()
{
M_itimes = 0;
}

Clxtest::~clxtest ()
{}

void Clxtest::output () const
{
cout << "Output for test!" << Endl;
m_itimes++;
}

int Clxtest::getoutputtimes () const
{
return m_itimes;
}

void outputtest (const clxtest& LX)
{
cout << LX. Getoutputtimes () << Endl;
Lx. Output ();
cout << LX. Getoutputtimes () << Endl;
}
The counter m_itimes is modified by mutable, so it can break through the const limit and can be modified in the const-modified function.
  
  


Another example is as follows:

  

Mutable used to modify member variables in a const member function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.