const and mutable declarations in one union

BCS ao at pathlink.com
Tue Jan 13 16:18:57 PST 2009


Reply to Sergey,

> 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

cost = "you can't chnage it"
invariant = "Will not change at all"

> 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.
> 

Setting it in a decleration my put it in read only memeory or even hard code 
it into expressions





More information about the Digitalmars-d mailing list