volatile vs. mutable in C++

前端 未结 6 1913
旧时难觅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条回答
  •  萌比男神i
    2020-12-04 05:57

    mutable: The mutable keyword overrides any enclosing const statement. A mutable member of a const object can be modified.

    volatile: The volatile keyword is an implementation-dependent modifier, used when declaring variables, which prevents the compiler from optimizing those variables. Volatile should be used with variables whose value can change in unexpected ways (i.e. through an interrupt), which could conflict with optimizations that the compiler might perform.

    Source

提交回复
热议问题