What does the 'mutable' keyword do in C++?
Last updated
A
mutabledata member can be modified even when the object isconst(insideconstmember functions or via const references).Typical uses: caching/memoization,
mutable std::mutex, counters and lazy flags that don't change the object's observable value.Use it only for non-logical state. Mutating real state through it breaks the const contract.
- https://www.geeksforgeeks.org/interview-prep/c-fundamentals-interview-questions-1/
- https://github.com/MAZHARMIK/OOP_Crash_Course_Cpp
- https://www.geeksforgeeks.org/cpp/storage-classes-in-c-with-examples/
- https://www.scribd.com/document/463735120/C-mutable-keyword
- https://stackoverflow.com/questions/136880/sell-me-const-correctness
- https://www.codecademy.com/resources/docs/cpp/variables/mutable-variables