C + + keyword mutable deep parsing _c language

Source: Internet
Author: User

1, about mutable keyword
First of all, the mutable keyword can only modify non-static and very variable member variables, and the values of the member variables decorated with mutable can be modified in the const function.

For example, the following code:

Copy Code code as follows:

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


The error occurs when compiling, because the const member function modifies the member variable, but it is OK to add the keyword mutable if the declaration is m_naccess.

PS: The state of an object is determined by the Non-static data member of the object, so the state of the image changes as the data member changes! If the member function of a class is declared as a const type, Indicates that the function does not change the state of the object, that is, the function does not modify the Non-static data members of the class. However, there are times when you need to assign values to the data members of a class in the class function. This time you need to use the mutable keyword.

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.