C + + Essentials (1) mutable keywords

Source: Internet
Author: User

        • mutable semantics
        • Const semantics
        • The relationship between mutable and const
        • Flexibility provided by mutable

mutable semantics

In C + +, mutable is set to break through the limitations of Const.
Variables modified by mutable will always be in a mutable state, even if the struct variable or class object is const in a const function, and its mutable members can be modified.

For example:

#include <iostream>classtester{ Public: Tester (intXintY): A_normal (x), b_mutable (y) {}voidPrintall ()Const{STD::cout<<"A_normal="<< A_normal <<"; B_mutable= "<< b_mutable <<STD:: Endl; }intA_normal;mutable intb_mutable;};intMain () {ConstTester TST (2,3); Tst.a_normal = A; Tst.b_mutable = -; Tst.printall ();return 0;}

Compile error, error message is as follows

test4mutable.cpp:In function ' int main () ': Test4mutable.cpp:20:error:assignment of Data-member ' Tester::a_normal ' in Re AD-ONLY structure

We will comment out the error line code and recompile, by running.

Mutable only non-static data members can be decorated in a class.

Const semantics

A const-modified variable will not change its value under any circumstances.
A const-modified function cannot directly or indirectly alter the value of a variable outside of any function body, even if a function that might cause such a change is called.

For a detailed analysis of the Const keyword, please refer to the blog: the const keyword analysis

The relationship between mutable and const

Mutable modified variables will always be in a mutable state, even in a const function.
So mutable and const form a symmetric definition: one that never changes and one that is forever mutable.

Then there are 3 questions to consider:

    1. Why should I protect the member variables of a class from being modified?
    2. Why mutable to break the const blockade?
    3. The necessity of using the const and mutable keywords.

The member variables of the protection class are not modified in the member function to ensure that the logic of the model is correct, by using the Const keyword to avoid incorrectly modifying the state of the class object in the function. And the effect of using this member function can be more accurately predicted in all places where the member function is used.

The mutable is designed to break through the const blockade, allowing some of the secondary or auxiliary member variables of the class to be changed at any time.

Without the use of the const and mutable keywords, of course, the const and mutable keywords just give modeling tools more design constraints and design flexibility, and programmers can put more logic checking issues to the compiler and modeling tools to ease the burden on programmers.

Flexibility provided by mutable

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, if you want to modify some data members that are independent of the class state in the Const function, you can use mutable to decorate the member.
Using the mutable keyword wisely, you can hide implementation details from the user without using something that is not deterministic and improve the quality of the code.

For example:

#include <iostream>classtester{ Public: Tester (intXintY): A_normal (x), b_mutable (y), PrintCount (0)    {}voidPrintall ()Const{STD::cout<<"A_normal="<< A_normal <<"; B_mutable= "<< b_mutable <<STD:: Endl;STD::cout<<"Print"<< ++printcount<<"Times"<<STD:: Endl <<STD:: Endl; }intA_normal;mutable intb_mutable;mutable intPrintCount;};intMain () {ConstTester TST (2,3); Tst.b_mutable = -;    Tst.printall ();    Tst.printall (); Tst.printall ();return 0;}

In the above example, the Printall function is a const function, but it is desirable to record the number of prints at the same time, so that a member variable PrintCount is added to the class, which is independent of the state of the class itself and needs to be changed in the Printall function. Therefore, the mutable keyword decoration is used to achieve the goal of statistical printing times.

The execution results are as follows:




Resources:
C + + Essentials full Mastery (v)--mutable, volatile

C + + Essentials (1) mutable keywords

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.