Re: const and ABCs
seanf wrote:
Hi group
Should I ever declare pure virtual functions const in an ABC?
Yes, when the method is not intended to change the state of the object.
Whether or not implementations change their state seems to be none of
the interface's business. Also, if I get it wrong, I can't un-const a
function in future without perhaps breaking existing callers.
I do have a few occasions where it is optional whether or not the
derived class will change its state. In those occasions I make the
function non-const.
For those other occasions where the change of state is only
implementation detail, there is always mutable.
On the other hand, if I don't, I can end up with embarrassingly
non-const getName() and the like in an otherwise const-correct
implementation class.
The answer lies in whether if you call getName() twice in a row without
any other method being called, the answer will be the same both times.
If you'd expect the answer to always be yes then the method is const,
even if the class that implements it retrieves the value from somewhere
remote on the first instances and caches it, and then on the 2nd
instance simply returns the cached value.
If getName() runs through a list and thus returns you different names
each time you call it, then effectively it's a logical change of state
and the method is logically non-const.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]