C ++ keyword mutable

Source: Internet
Author: User

Mutable

(1) mutable means "variable, variable", which is the opposite of const in C ++.

(2) 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

Instance description:

 # include 
   
   
using namespace STD;
class tew.utable
{< br> Public :< br> tew.utable () {I = 0 ;}< br> int output () const
{< br> return I ++; // error c2166: l-value specifies const object
}< br> private :
int I;
};
int main ()
{< br> tew.utable;
cout return 0 ;< BR >}

Obviously, I ++ cannot compile the const modifier function.

 
# Include <iostream>
Using NamespaceSTD;

ClassTestmutable
{
Public:
Testmutable () {I =0;}
IntOutput ()Const
{
ReturnI ++;
}
Private:
MutableIntI;
};

IntMain ()
{
Testmutable;
Cout <testmutable. Output () <Endl;
Return 0;
}

After mutable is added to int I, the compilation is passed, and the function of the keyword mutable can be seen immediately.

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.