(C++) mutable
February 24, 2017 · View on GitHub
(C++) mutable
mutable is a Keyword to indicate that a class variable can be changed in a const member function.
In class design mutable variables say nothing about a class, but are used for bookkeeping tasks.
Example
When a neural network responds to a certain input, it will produce a certain output. The flow of information from input layer to output layer is called propagation. During propagation, the neural network must not be changed. Therefore, in class design the propagation member function must be a const member function. If the last input must be stored (for back-propagation for example), this must be done with mutable.
struct Input { /* some structure to store neural net inputs */ }; struct Output { /* some structure to store neural net outputs */ }; struct NeuralNet { //Propagate must be a const method const Output Propagate(const Input& input) const { m_last_input = input; //Store last input //Perform actual propagation } mutable Input m_last_input; //Last input must be mutable };