I have following understanding of mutable variables in C++.
My question is in terms of memory. Where will compiler store mutable variable in memory? Since its modifiable, then it will not be "read only" memory for sure.
mutable (mutable) is just a type qualifier for the compiler like const or volatile.
The members of a class are stored on a continuous chunk of memory (except for the static ones). If you define a member as const, it doesn't mean that the compiler will place it in a RO memory. const declaration won't effect anything during the run time, it is just a keyword for the compiler to perform appropriate checks and optimizations during compilation.
Once you define a class (or a method) as const but you still need to modify a specific member of that class (like mutex or like you mentioned - to cache a value) you let the compiler know that this specific member is mutable, otherwise you will get a compilation error.
Presumably this is compiler specific but I imagine most compilers would simply choose to always locate classes (the entire class) with mutable members into non-const memory to allow for this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With