In embedded systems, many driver APIs are marked const because calling them does not modify the device’s logical or externally observable state.
However, such functions may still update internal bookkeeping values, such as access counters or debug metrics.
These internal changes are allowed because they do not change the logical state of the object (e.g., hardware configuration, register contents, outputs). They only update metadata that is invisible to external users of the object.
Your task is to modify the class so that the logAccess() function can update an internal counter, even though both the function and the object are declared const.
What you must do:
logAccess() N times on a const object.logAccess() must successfully increment the counter, even though it is a const function.N calls, getCount() must return N.N = 0, no calls should be recorded, and the output must be 0.Although logAccess() modifies a variable, it only updates internal bookkeeping, not the logical external state, which makes this valid inside a const function.
Important:
Do not use incorrect approaches such as:
const_caststaticconst keywordThese break const-correctness.
There is a correct and safe C++ mechanism to allow internal updates.
Example
Input:
3
Output:
3
Constraints:
logAccess() as a const function.main() const.
Input
0
Expected Output
0