need 'this' to access data member
clayasaurus
clayasaurus at gmail.com
Wed Sep 27 09:15:46 PDT 2006
Derek Parnell wrote:
> On Tue, 26 Sep 2006 22:22:05 -0500, clayasaurus wrote:
>
>> Hi, I have the following code attached, and I do not understand why I
>> can not access this data member. Help is very much appreciated, thanks.
>
> As explained already, a named union/struct must also have a data
> declaration before you can refer to its members. However, have you
> considered anonymous union/struct ? When you use these, you can refer to
> the members without having to qualify their names.
>
> import std.stdio;
> // datafield hacks
> alias int FILE;
> alias int Uint8;
>
> // structure
> struct SDL_RWops
> {
>
> int (*seek)(SDL_RWops *context, int offset, int whence);
> int (*read)(SDL_RWops *context, void *ptr, int size, int maxnum);
> int (*write)(SDL_RWops *context, void *ptr, int size, int num);
> int (*close)(SDL_RWops *context);
> uint type;
>
> union
> {
> version(Windows)
> {
> struct
> {
> int append;
> void *h;
> }
> }
> struct
> {
> int autoclose;
> FILE *fp;
> }
> struct
> {
> Uint8 *base;
> Uint8 *here;
> Uint8 *stop;
> }
> struct
> {
> void *data1;
> }
> }
> }
>
>
> int main()
> {
> SDL_RWops w;
>
> // need 'this' to access data member data1??
> std.stdio.writefln("autoclose A:" , w.autoclose);
>
> w.data1 = &w; // sample data
> std.stdio.writefln("autoclose B:" , w.autoclose);
>
> version(Windows)
> {
> w.append = 42; // sample data
> std.stdio.writefln("autoclose B:" , w.autoclose);
> }
> return 0;
> }
>
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.
More information about the Digitalmars-d-learn
mailing list