72. mutable Keyword

Question.3

A Config class computes an expensive CRC only when first requested:

class Config {
   uint8_t data[64];
   mutable uint32_t cached_crc;
   mutable bool crc_valid = false;
public:
   uint32_t get_crc() const {
       if (!crc_valid) {
            cached_crc = compute_crc(data, 64);
            crc_valid = true;
       }
       return cached_crc;
   }
};

What pattern does this implement?

Need Help? Refer to the Quick Guide below

Select Answer