A Mutable Object Member

When a data member is declared mutable, then it is legal to assign a value to it from a const member function. The following example may demonstrate when it is needed:

 class Buffer {	void * data;  //raw data buffer to be transmitted on the net	size_t size;  //size of  the data 	mutable int crc; //used to verify that no errors occurred during transmission;//a mutable variable is allowed to be modified from a const //member function. 	public:	//...	int GetCrc() const;	void Transmit() const; //copmutation of crc_val should be done here};void f() {Buffer buffer;//...fill buffer with databuffer.Transmit(); //crc can be modified here; other members may not}

Class buffer uses a data buffer transmitted via communication link after it has been filled. The filling process is quite slow and repetitive, so there’s no point in calculating the value of crc every time a few bytes are appended to data. On the other hand, the receiving size has to get the right crc of the data in order to ensure that no corruption has occurred along the way. Naturally, the computation of crc has to be done in Transmit(), right before sending buffer. Since Transmit()is declared as a const member function for obvious reasons, the data is not to be changed when transmitted. In order to allow assignment of the correct value to crc right on time, the crc member is defined mutable, and hence, can be modified even by a const member function such as Transmit().

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may