72. mutable Keyword

Question.1

A Sensor class has a const read method that needs to update an access counter:

class Sensor {
   int pin;
   mutable int access_count;
public:
   int read() const {
       access_count++;  // Modifying in const method
       return hardware_read(pin);
   }
};

Will access_count++ compile inside a const method?

Need Help? Refer to the Quick Guide below

Select Answer