Mutable Counter in Const

#include <iostream>
using namespace std;

class AccessTracker {
public:
    AccessTracker() : count(0) {}

    void logAccess() const {
        count++;   // this must be allowed
    }

    int getCount() const {
        return count;
    }

private:
    mutable int count;     // modify this so logAccess() can update it
};

int main() {
    int n;
    cin >> n;

    const AccessTracker tracker;   // must remain const

    for (int i = 0; i < n; i++) {
        tracker.logAccess();
    }

    cout << tracker.getCount();
    return 0;
}

Solving Approach

 

 

 

 

 

Upvote
Downvote
Loading...

Input

0

Expected Output

0