Mutable and volatile keywords: mutablevolatile

Source: Internet
Author: User

Mutable and volatile keywords: mutablevolatile

1. mutable

In C ++, mutable is set to break through the const restrictions. Variables modified by mutable will always be in a mutable state. Even if the struct variable or class object in a const function is const, its mutable member can be modified. Mutable can only modify non-static data members in the class.

# Include <iostream> using namespace std; class test {mutable int a; int B; public: test (int _ a, int _ B): a (_ ), B (_ B) {}; void fun () const // fun is a const function. You cannot modify the data member of the class object. However, because a is mutable, it can be modified, however, you cannot modify B {a + = B;} void print () {cout <a <"," <B <endl ;}};

We know that if the member function of the class does not change the state of the object, the member function will be declared as const. However, sometimes we need to modify some data members irrelevant to the class status in the const function, so this data member should be modified by mutalbe.

2. volatile

Volatile was originally intended to be "changeable", but this interpretation is a bit misleading and should be interpreted as "direct access to the original memory address" is more appropriate. "Easy to change" is an unknown change in the value of a common variable in terms of the compiler (optimization function) (that is, it is not a situation where the value is changed by executing the code assignment ), it is caused by external factors, such as multithreading and interruptions. When the compiler is optimizing, it sometimes retrieves some values and directly accesses them from the registers rather than from the memory. This optimization is no problem in a single-threaded program, however, in a multi-threaded program, because multiple threads run concurrently, it is possible that a thread has changed a public variable, and the value of registers in other threads has expired, but this thread itself does not know, think that there is no change, still get from the register, it will cause the program to run undefined behavior. It is not because the variable modified with volatile is "changeable". If there is no external cause, it will not change if it is defined with volatile. With the variable modified by volatile, the compiler will not optimize its related code, but generate the corresponding code to directly access the original memory address.

Generally, volatile is used in the following areas:

1. volatile must be added to the variable modified in the interrupted service program for testing by other programs;

2. volatile should be added to the labels shared by all tasks in a multi-task environment;

3. volatile is also required for memory-mapped hardware registers, because each read/write operation may have different meanings;

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.