const and mutable declarations in one union

Sergey Kovrov kovrov+digitalmars at gmail.com
Tue Jan 13 16:08:46 PST 2009


Consider following definition:

|class Node
|{
|    union
|    {
|        protected Rect _rect;
|        const struct
|        {
|            short x, y;
|            ushort width, height;
|        }
|    }
|    setRect(...) { ... }
|}

The point is to have a readonly view of data (x, y, width, height), 
protected mutable view of same data (_rect), and an interface to set it 
publicly (setRect).

1. Is this declaration is legal? (const and mutable definitions in one 
union)
2. Is there a difference between const and invariant struct? (I guess it 
should)

The current implementation seems yielding undefined behavior in this 
case. If I set initial const values in constructor everything works as 
expected. If I set initial const values in definition, the whole union 
seems to be immutable, eg. setting ``_rect`` do not have an effect.


-- serg.



More information about the Digitalmars-d mailing list