Fully transitive const is not necessary

Bill Baxter dnewsgroup at billbaxter.com
Wed Apr 2 13:43:35 PDT 2008


Janice Caron wrote:

> 
>>  - a pure method cannot access the mutable portion of a logically invariant
>>  data value.
> 
> Well, the easy way to do that is to redesign the class into two
> classes, one const and the other not. That way, you get the exact same
> benefit, but in addition, you end up saying what you mean, instead of
> writing obfuscated code.

I don't understand how to do what you're saying.  Here's the version 
using mutable:

class Calc
{
    private mutable int result_cache;
    int calc_result(int) { .../*uses result_cache to save results*/ }
    ...
}
auto x = new Calc;
int y = x.calc_result(32);

So how do you separate that into two classes?  How do I create an 
instance of that two-class thing?  Via a third class?  How can Calc 
access its cache?  The user has to pass in the cache to every call to 
calc_result?   The goal is for the caching to be an implementation 
detail that users need not worry about.  The above seems about as 
straightforward as it gets.  Any else is probably going to have to come 
out *more* obfuscated.

--bb



More information about the Digitalmars-d mailing list