SImple C++ code to D

bearophile via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jul 14 07:02:13 PDT 2014


Alexandre:

> 	BYTE[8] Name;

Generally in D field names start with a lowercase (unless you 
need them with uppercase).


> Btw, my problem is, how to acess the union elements ?
>
> I try this:
> //...
> 	scth[0].Misc.VirtualSize = 15;
> //...
>
> But, the compiler return that error:
> main.d(151): Error: need 'this' for 'VirtualSize' of type 'uint'

The error message is not the best. It is saying you are not 
accessing data, just its definition. So you need to instantiate 
the union:

struct Foo {
     ubyte[8] name;
     union Bar {
         ushort physicalAddress, virtualSize;
     }
     Bar b;
}

void main() {
     Foo f;
     f.b.physicalAddress = 10;
}


Or use an anonymous one:

struct Foo {
     ubyte[8] name;
     union {
         ushort physicalAddress, virtualSize;
     }
}

void main() {
     Foo f;
     f.physicalAddress = 10;
}

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list