Logical const

Steven Schveighoffer schveiguy at yahoo.com
Wed Dec 1 13:16:55 PST 2010


On Wed, 01 Dec 2010 11:09:08 -0500, so <so at so.do> wrote:

>> Most likely not.  How do you say that the 'draw' function switches the  
>> widget to a different parameterized type?  With const, you can just  
>> slap a const on the end of the function.
>>
>> Here is some example of what I mean:
>>
>> class Widget
>> {
>>     mutable Window location;
>>     int x, y, width, height;
>>     void draw() const { location.drawRectangle(x, y, width, height); }
>> }
>>
>> where Window.drawRectangle is not a const function.
>>
>> -Steve
>
> Looking at the replies seems i am the only one that didn't quite get the  
> example.
>
> 1. Why do you call it "location". Is it something like a context/handle?

It is where the widget is located, i.e. a physical window where the widget  
is drawn.

> 2. Why does Widget have a draw function?

Really?

> 3. If the Window doesn't provide a const draw function, why do you  
> insist on having one? Because Widget actually doesn't change?
> Well in this example it does and if the details of drawRectangle is not  
> available to you, that change might be a serious one.

What changes?  x, y, width and height do not change.  That is considered  
part of the widget's state.  The location is part of the state but not  
completely, only the association with the location is considered part of  
the state.  This is not exactly conveyed in the type, if we have tail-  
references then we could use tail-mutable references as well, which means  
we get head-const :)

Note that drawRectangle does not take any references, so there is no  
possibility that any of widget's state changes.  That is provable.

-Steve


More information about the Digitalmars-d mailing list