I have a method (in C++) which generates a value based on a parameter and the parameters from previous calls. Calling it more than once with the same parameter may generate different values each time. For example:
public:
int getValue(const int param)
{
int value = param ^ previousParam;
previousParam = param;
return value;
}
private:
int previousParam;
Does it make sense to allow this method to be called on const
objects? Is previousParam
part of the object's internal state, or does it "leak" since the internal state affects the object's external behavior?
I think of this method like a generator that just creates a series of values on subsequent calls, rather than a method which modifies the object. The caller is not supposed to depend on a particular parameter generating a specific value. Would it be sensible to change the declarations to the following?
int getValue(const int param) const;
mutable int previousParam;