need 'this' to access data member
Mike Parker
aldacron71 at yahoo.com
Wed Sep 27 10:54:45 PDT 2006
clayasaurus wrote:
>>
>
> That would be nicer, but it is not my call. The SDL_RWops structure is
> from the SDL library (www.libsdl.org) and the goal is to stay as close
> to the original structure as possible.
>
>
In C, named inner structs and unions are treated as fields.
This...
##############
typedef struct
{
struct foo
{
int x;
}
} MyStruct;
MyStruct ms;
ms.foo.x = 0;
##############
...is identical to this...
##############
typedef struct
{
int x;
} Foo;
typedef struct
{
Foo foo;
} MyStruct;
MyStruct ms;
ms.foo.x = 0;
##############
Assuming a 32 bit C compiler, both sizeof(MyStruct) in both examples
above should output '4'.
In D, inner structs and unions are treated as type declarations but not
members. Here is the same example in D:
##############
struct MyStruct
{
struct foo
{
int x;
}
}
MyStruct ms;
ms.foo.x = 0; // Error
#############
MyStruct.sizeof will output 1 in this example. Not only is foo not
accessible as a member field, MyStruct is incompatible with the C side
of the house. The following version fixes both issues:
##############
struct MyStruct
{
struct Foo
{
int x;
}
Foo foo;
}
MyStruct ms;
ms.foo.x = 0;
##############
Now the output of MyStruct.sizeof should be the same as the C version.
DerelictSDL was a tedious port, so I'm surprised more silliness like
this hasn't turned up yet. I'm glad you caught it.
More information about the Digitalmars-d-learn
mailing list