I have a C++ class A
that can be constructed to perform a certain computation using a function A::compute
. This function requires to write to a preallocated memory area (working area) that was allocated at construction of A
to perform this computation efficiently. I would like to A::compute
to be const
in relation to the class, because the computation does not alter the logical state of the object.
Is this a case when the keyword mutable
should be used?
Example code:
class A {
public:
A(size_t size) : m_workingArea(size) {}
int compute(const std::vector<int>& data) const {
// ... checks ...
std::copy(data.begin(), data.end(), m_workingArea.begin());
// ... compute ...
// return a result, say first element of the working area
return m_workingArea[0];
}
private:
mutable std::vector<int> m_workingArea;
};