Some quick questions

BCS ao at pathlink.com
Wed Aug 8 07:25:20 PDT 2007


Reply to Márcio,

> Thanks for all your replies. I have one more quick question: how do I
> access union's members, inside a struct? For example:
> 
> ;struct S {
> ;	union u {
> ;		ubyte b;
> ;		uint i;
> ;	}
> ;}
> ;S s;
> ;ubyte b = s.u.b;    // Error: need 'this' to access member b
> Thanks!
> 

;struct S {
;	union u {
;		ubyte b;
;		uint i;
;	}
;}
;S s;
;ubyte b = s.u.b;    // Error: need 'this' to access member b

 The reason that doesn’t work is that the uses of union there is a type deceleration, 
not a member declaration. So you get a struct without any content.

option 1 use anonymous unions

;struct S {
;	union {
;		ubyte b;
;		uint i;
;	}
;}
;S s;
;ubyte b = s.b;

option 2:

;struct S {
;	union U {
;		ubyte b;
;		uint i;
;	}
;	U u;
;}
;S s;
;ubyte b = s.u.b;




More information about the Digitalmars-d-learn mailing list