A const member function needs to update a cache counter. Which change is legitimate, and why?
Correct answer
C. Mark the member mutable, so that a const member function can still modify it
15 views
C. Mark the member mutable, so that a const member function can still modify it
15 views
mutable exempts a data member from the constness of the object, so a const member function can legitimately update caches, mutexes and reference counts. Casting away const and writing is undefined behaviour when the object genuinely is const, as globals and const instances are. Mutable members are still not thread-safe, so a concurrent cache needs a mutex or atomics.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is C? Ask here and get an explanation.