Keyword parsing-volatile and mutable

Source: Internet
Author: User
Volatile indicates "volatile, variable, and unstable" in English ". C/C ++ uses this term as a qualifier, which tells the compiler that a variable may be changed out of direct control, so do not perform optimization actions. Its syntax is the same as that of const, and it is an additional modifier for the type. Volatile int display_register;
Volatile Task * curr_task;

Mutable in English means "variable and changeable". Although it is a little similar to volatile in terms of meaning, mutable serves as a keyword but has another purpose.
Mutable is a keyword in C ++, but not a keyword in C. We know that a common variable cannot be modified in the const member function, but sometimes it is expected that some data members can be modified, which can be defined as mutable. Mutable data members can never be const, even when they are members of a const object. Class Screen ...{
Public:
// Interface member functions
Private:
Mutable size_t access_ctr; // may change in a const members
// Other data members as before
};

Void Screen: do_display (std: ostream & OS) const ...{
++ Access_ctr; // keep count of callto any member function
OS <contents;
}

Refer:
Stanley B. Lippman, Joseph Lajoie, Barbara E. Moo. (translated by Li shixian Jiang Aijun Li Xiaoyong Lin) C ++ primer Chinese edition

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.