[Insight-developers] Re: FunctionBase and const

Luis Ibanez ibanez@cs.unc.edu
Tue, 18 Sep 2001 17:29:29 -0400


This seems to be already solved,
but just for information...


If eventually you *really* need to have
some internal variable that change during
the execution, and you consider that this
variable is *not* part of the state of your
class, you can use "mutable" when declaring
the variable.


For example:


class myClass {
private:
  int         IamStateVariable1;
  float       IamStateVariable2;
  mutable int ImNotPartOfTheState;

  double Compute(const double & value) const {
      ImNotPartOfTheState++;
      return value*value;
   }

};


The "mutable" keyword will let you change the 
value of the member variable even though you
are within a "const" method.

This is the case for example in the internal
clock that itk and vtk use for keeping the 
modified time in the data pipeline each time
a ProcessObject is executed.

There are very few cases in which the use of
"mutable" is acceptable. Think three times 
before using it. The State machine model is the 
politically correct way of designing classes,
and the use of "mutable" break this model.


In general, your code is much better if you 
follow a good use of const, as Jim and Josh 
have already explained. 




Luis