volatile vs. mutable in C++

前端 未结 6 1919
旧时难觅i
旧时难觅i 2020-12-04 05:26

I have a question about the difference between volatile and mutable. I noticed that both of the two means that it could be changed. What else? Are they the same thing? What\

6条回答
  •  心在旅途
    2020-12-04 05:59

    They are definitely NOT the same thing. Mutable interacts with const. If you have a const pointer, you normally could not change members. Mutable provides an exception to that rule.

    Volatile, on the other hand, is totally unrelated to changes made by the program. It means that the memory could change for reasons beyond the control of the compiler, therefore the compiler has to read or write the memory address every time and can't cache the content in a register.

提交回复
热议问题