const and mutable declarations in one union

Daniel Keep daniel.keep.lists at gmail.com
Tue Jan 13 17:18:10 PST 2009



Sergey Kovrov wrote:
> On 1/14/2009 2:18 AM, BCS wrote:
>> cost = "you can't chnage it"
>> invariant = "Will not change at all"
> ....
>> Setting it in a decleration my put it in read only memeory or even hard
>> code it into expressions
> 
> Well this is usually oblivious, but not in case of union. Which might be
> a little bit different.. I have an impression that invariant may put
> data to readonly section, but not const.
> 
> This of course might be just an invalid usage of union from programmer's
> side, which righteously yeld undefined behavior. but I was hoping for a
> special treat of const (not invariant) in context of union.
> 
> 
> -- serg.

Yeah, if this doesn't cause compilation to fail, I'm pretty sure it
should.  You're giving the compiler two contradictory statements
regarding the mutability of that data.

Rather, why not use this?

> class Node
> {
>     protected Rect _rect;
>
>     short x() { return _rect.x; }
>     short y() { return _rect.y; }
>     ushort width() { return _rect.width; }
>     ushort height() { return _rect.height; }
>
>     setRect(...) { ... }
> }

This will have more or less the same outward-facing interface, whilst
actually working.

Incidentally, I've haven't used opDot before, but this might also work:

> class Node
> {
>     protected Rect _rect;
>
>     const Rect* opDot() { return &_rect; }
>
>     setRect(...) { ... }
> }

Finally, if you have questions on how to do something, please direct
them to the D.learn newsgroup in future.

  -- Daniel



More information about the Digitalmars-d mailing list