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