C ++ mutable

Source: Internet
Author: User

[Conversion] The Chinese meaning of the mutable keyword is: "variable, variable" and constant (that is, const in C ++) are antonyms. Mutable in C ++ is set to break through the const limitation. Variables modified by mutable will always be in a mutable state, even in a const modified function. Mutable is also a strange modifier (specifier), which can only be used for non-static data members of a class. Next I will discuss the semantics and usage of mutable, but first I will explain a key concept of the C ++ object model.

Object status
The state of an object consists of the values of its non-static data members. Therefore, modifying a data member changes the state of the entire object. Declaring a member function as const ensures that it does not change the object state.

However, in some cases, the logical state of an object may be different from its physical state. For example, this situation exists for an object that represents a painting image. If the image has not been changed, we think its status has not changed. However, in terms of underlying implementation, if large objects are not active for a period of timeMemoryIt is usually exchanged into a file. Switching an image does not really affect its status, but some data members of the object may change. Here, the pointer and flag may change.

When a user calls a const member function such as redraw (), they do not care about how the function is implemented internally. From their perspective, this function does not change the logical state of the object, so it is declared as Const. Redraw () may modify the physical state of an object, which is actually an implementation detail that they should not care about. For example:

int image: redraw () const
{< br> If (isloaded = false)
{< br> //.. read image data from a disk into a local buffer
isloaded = true; // changing a data member's value
}< br> //.. paint image in the screen
}

Mutable data member
If you try to compile this sectionCode, You will get a compilation error. Although redraw () is declared as const, it modifies a data member. To solve this compilation error, declare isloaded as a mutable data member:

Class image {
Public:
Int redraw () const;
//..
PRIVATE:
Mutable bool isloaded; // can be changed by a const Function
};

Unlike common data members, the const member function can modify mutable data members.

The use of mutable data members seems like a scam because it enables the const function to modify the data members of an object. However, the wise use of the mutable keyword can improve the code quality, because it allows you to hide implementation details to users without using uncertain things, such as const_cast <>.

The Chinese meaning of mutalbe is "variable, easy to change", which is the opposite of constant (const in both C ++.

In C ++, mutable is also set to break through the const restrictions. Variables modified by mutable will always be in a mutable state, even in a const function.

We know that if the member function of the class does not change the state of the object, the member function will be declared as Const. However, sometimes we need to modify some data members irrelevant to the class status in the const function, so this data member should be modified by mutalbe.

Below 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 output function of the clxtest member function is used for output and does not modify the status of the class. Therefore, it is declared as Const.

The outputtest function is also used for output. It calls the output method of the object lx. To prevent other member functions from being called to modify any member variables, the parameter is also modified by const.

Now, we need to add a function: calculate the number of output times of each object. If the variable used for counting is a common variable, the value of the variable cannot be modified in the output of the const member function. The variable has nothing to do with the state of the object, therefore, the const attribute of output should be removed to modify the variable. At this time, our mutable will be available-as long as mutalbe is used to modify this variable, all the problems will be solved.

The modified code is as follows:

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 mutable, so it can break the const restriction and be modified in the function modified by const.

Related Article

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.