#include <iostream>
using namespace std;
class AccessTracker {
public:
AccessTracker() : count(0) {}
// const function allowed to update internal bookkeeping
void logAccess() const {
count++;
}
int getCount() const {
return count;
}
private:
mutable int count; // allows modification inside const functions
};
int main() {
int n;
cin >> n;
const AccessTracker tracker;
for (int i = 0; i < n; i++) {
tracker.logAccess();
}
cout << tracker.getCount();
return 0;
}
Explanation & Logic Summary:
const member function promises not to modify the object’s logical external state, such as hardware registers or outputs.mutable.Firmware Relevance & Real Embedded Context:
Many embedded drivers update internal counters or cached values inside const functions.
Example:
class TempSensor {
public:
int readTemperature() const {
accessCount++; // allowed: internal bookkeeping
return i2cReadRegister(TEMP_REG);
}
private:
mutable uint32_t accessCount;
};
Common real-world uses:
const driver APIsKeeping the driver object const is common to prevent accidental modification of hardware settings by application code.
Input
0
Expected Output
0