A Mutable Object Member

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:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of