offsetof

Steven Schveighoffer schveiguy at yahoo.com
Thu Aug 21 12:54:06 PDT 2008


"Heinz Traub" wrote
> Hi,
>
> I've been doing Direct Input code and need to port C to D. I'm trying to 
> get the offset of a struct member 
> (http://www.digitalmars.com/d/1.0/ctod.html#fieldoffset) but when i do 
> this:
>
> module dinputd;
> struct DIMOUSESTATE
> {
> long lX;
> long lY;
> long lZ;
> byte rgbButtons[4];
> }
>
> ...
> module dinputdriver;
> case DIMOUSESTATE.lX.offsetof:
>
> I get the following error:
>
> Error: this for lX needs to be type DIMOUSESTATE not type 
> dinputdriver.Mouse.
>
> if i call offsetof in dinputd(the same module where DIMOUSESTATE struct is 
> declared) i get the following error:
>
> dinputd.d(89): Error: no property 'lX' for type 'DIMOUSESTATE'
>
> Damn! i'm doing everything as specified by the docs. But i can't get it to 
> work.
>
> Can someone help me please. Thanks in advance.

This code compiles:

module dinputd;
struct DIMOUSESTATE
{
    long lX;
    long lY;
    long lZ;
    byte rgbButtons[4];
}

int main(char[][] args)
{
    switch(args.length) // just to have a non-constant integer
    {
    case DIMOUSESTATE.lX.offsetof:
        break;
    }
}

This does not:

module dinputd;
struct DIMOUSESTATE
{
    long lX;
    long lY;
    long lZ;
    byte rgbButtons[4];
}

class Mouse
{
    this(int i)
    {
        switch(i)
        {
        case DIMOUSESTATE.lX.offsetof:
            break;
        }
    }
}

With a similar error as what you said originally.

I think this is a bug in DMD.  You should file a bug report: 
http://d.puremagic.com/issues/enter_bug.cgi?product=D

I can't think of a sensible workaround (besides saving the offsets in a 
static function, then using those in the member function).

-Steve 




More information about the Digitalmars-d-learn mailing list