Logical const

so so at so.do
Wed Dec 1 08:09:08 PST 2010


> 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?
2. Why does Widget have a draw function?
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.

IMO for this particular example mutable just encourages a bad design.
On the other hand, mutable mutex is clear and reasonable since threads are  
different from usual program flow. Which as Sean said we have a solution.
I don't think we can have "mutable" within current const design. With the  
vast differences between D and C++ const system we shouldn't have it  
anyhow.

One another thing i don't quite get is why cast from immutable/const is  
allowed.
Is it because of the cases that we know the function doesn't change  
anything but still lacks the immutable/const signature?

Thank you!

-- 
Using Opera's revolutionary email client: http://www.opera.com/mail/


More information about the Digitalmars-d mailing list