Hide implementation details using mutable members

Source: Internet
Author: User

The mutable keyword is a strange modifier (specifier) that 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 that of a base 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, at the underlying implementation level, if large objects are not active for a period of time, their memory 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
{
If (isloaded = false)
{
//. Read image data from a disk into a local buffer
Isloaded = true; // changing a data member's Value
}
//. Paint image in the screen
}

Class Image

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 <>.

Author: Danny Kalev, a system analyst and software engineer, has 14 years of experience in C ++ and object-oriented design.

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.