Thread overview
named union : needs 'this' to access member error
Jun 18, 2005
David L. Davis
Jun 18, 2005
Derek Parnell
Jun 18, 2005
Tom S
Jun 18, 2005
David L. Davis
Jun 18, 2005
Tom S
Jun 18, 2005
David L. Davis
Re: [OT] named union : needs 'this' to access member error
Jun 18, 2005
Tom S
June 18, 2005
# // needthis.d
# // WinXP SP2, D v0.127
# // BUG: "need 'this' to access member AsciiChar"
# // Seems like a named union error, because if it's
# // unnamed it works just fine, but it's a MS struct
# // so I need it the way it is.
# private import std.c.windows.windows;
#
# extern( Windows )
# {
# struct CHAR_INFO
# {
#     union Char  //<-- if Char is commented out...ok
#     {
#         WCHAR UnicodeChar;
#         CHAR  AsciiChar;
#     }
#
#     WORD Attributes;
# };
# }
#
# int main()
# {
#     CHAR_INFO chiFill;
#     chiFill.Char.AsciiChar = cast(char)'\x20'; //<--need 'this' to access
#                                                //   member AsciiChar
#     //chiFill.AsciiChar = cast(char)' '; //<-- works with Char commented out
#     return 0;
# }

Output:
----------
C:\dmd>dmd needthis.d
needthis.d(26): need 'this' to access member AsciiChar

C:\dmd>

David L.

-------------------------------------------------------------------
"Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
-------------------------------------------------------------------

MKoD: http://spottedtiger.tripod.com/D_Language/D_Main_XP.html
June 18, 2005
On Sat, 18 Jun 2005 02:24:55 +0000 (UTC), David L. Davis wrote:

> # // needthis.d
> # // WinXP SP2, D v0.127
> # // BUG: "need 'this' to access member AsciiChar"
> # // Seems like a named union error, because if it's
> # // unnamed it works just fine, but it's a MS struct
> # // so I need it the way it is.
> # private import std.c.windows.windows;
> #
> # extern( Windows )
> # {
> # struct CHAR_INFO
> # {
> #     union Char  //<-- if Char is commented out...ok
> #     {
> #         WCHAR UnicodeChar;
> #         CHAR  AsciiChar;
> #     }
> #
> #     WORD Attributes;
> # };
> # }
> #
> # int main()
> # {
> #     CHAR_INFO chiFill;
> #     chiFill.Char.AsciiChar = cast(char)'\x20'; //<--need 'this' to access
> #                                                //   member AsciiChar
> #     //chiFill.AsciiChar = cast(char)' '; //<-- works with Char commented out
> #     return 0;
> # }
> 
> Output:
> ----------
> C:\dmd>dmd needthis.d
> needthis.d(26): need 'this' to access member AsciiChar
> 
> C:\dmd>
Not a bug.

If you name a union, then you must create an instance of it before you can reference its members. If you don't name a union, then the union becomes its own instance.


 struct CHAR_INFO
 {
     union
     {
         WCHAR UnicodeChar;
         CHAR  AsciiChar;
     }
 }

In this case, the anonymous union is also an instance of itself so you can reference its members directly.

 CHAR_INFO chiFill;
 chiFill.UnicodeChar = '\x20';

However ...

 struct CHAR_INFO
 {
     union Char
     {
         WCHAR UnicodeChar;
         CHAR  AsciiChar;
     }
     Char aChar;
 }

In this case, the union is named so you have to create an instance of it first, then reference member through that instance.

 CHAR_INFO chiFill;
 chiFill.aChar.UnicodeChar = '\x20';


If you use the union name itself in the reference, then you need to only
reference 'static' members, and I'm not sure what that would be.

-- 
Derek
Melbourne, Australia
18/06/2005 12:46:21 PM
June 18, 2005
I believe it's intended to work this way... Make the union's name e.g. UChar and then declare it inside the struct, like:

# struct CHAR_INFO
# {
#     union UChar
#     {
#         WCHAR UnicodeChar;
#         CHAR  AsciiChar;
#     }
#
#     UChar Char;
#     WORD  Attributes;
# }



David L. Davis wrote:

> # // needthis.d
> # // WinXP SP2, D v0.127
> # // BUG: "need 'this' to access member AsciiChar"
> # // Seems like a named union error, because if it's # // unnamed it works just fine, but it's a MS struct
> # // so I need it the way it is.
> # private import std.c.windows.windows;
> # # extern( Windows )
> # {
> # struct CHAR_INFO # {
> #     union Char  //<-- if Char is commented out...ok
> #     {
> #         WCHAR UnicodeChar;
> #         CHAR  AsciiChar;
> #     }
> #     #     WORD Attributes;
> # };
> # }
> # # int main()
> # {
> #     CHAR_INFO chiFill;
> #     chiFill.Char.AsciiChar = cast(char)'\x20'; //<--need 'this' to access
> #                                                //   member AsciiChar
> #     //chiFill.AsciiChar = cast(char)' '; //<-- works with Char commented out
> #     return 0;
> # }
> 
> Output:
> ----------
> C:\dmd>dmd needthis.d
> needthis.d(26): need 'this' to access member AsciiChar


-- 
Tomasz Stachowiak  /+ a.k.a. h3r3tic +/
June 18, 2005
Damn, Derek was faster ;)


-- 
Tomasz Stachowiak  /+ a.k.a. h3r3tic +/
June 18, 2005
In article <trarz7rthih8.140fc8415dmrw$.dlg@40tude.net>, Derek Parnell says...
>
>Not a bug.
>
>If you name a union, then you must create an instance of it before you can reference its members. If you don't name a union, then the union becomes its own instance.
>
>
> struct CHAR_INFO
> {
>     union
>     {
>         WCHAR UnicodeChar;
>         CHAR  AsciiChar;
>     }
> }
>
>In this case, the anonymous union is also an instance of itself so you can reference its members directly.
>
> CHAR_INFO chiFill;
> chiFill.UnicodeChar = '\x20';
>
>However ...
>
> struct CHAR_INFO
> {
>     union Char
>     {
>         WCHAR UnicodeChar;
>         CHAR  AsciiChar;
>     }
>     Char aChar;
> }
>
>In this case, the union is named so you have to create an instance of it first, then reference member through that instance.
>
> CHAR_INFO chiFill;
> chiFill.aChar.UnicodeChar = '\x20';
>
>
>If you use the union name itself in the reference, then you need to only reference 'static' members, and I'm not sure what that would be.
> 
>-- 
>Derek
>Melbourne, Australia
>18/06/2005 12:46:21 PM

Thanks Derek...funny thing tho, this example is a part of some D code that compiled fine with D v0.97 in July 2004 ( somehow I missed retesting this code some 30 version of D ago :P ). Thanks again.

David L.

-------------------------------------------------------------------
"Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
-------------------------------------------------------------------

MKoD: http://spottedtiger.tripod.com/D_Language/D_Main_XP.html
June 18, 2005
In article <d902fe$2gfd$1@digitaldaemon.com>, Tom S says...
>
>I believe it's intended to work this way... Make the union's name e.g. UChar and then declare it inside the struct, like:
>
># struct CHAR_INFO
># {
>#     union UChar
>#     {
>#         WCHAR UnicodeChar;
>#         CHAR  AsciiChar;
>#     }
>#
>#     UChar Char;
>#     WORD  Attributes;
># }
>
>
>
>David L. Davis wrote:
>
>> # // needthis.d
>> # // WinXP SP2, D v0.127
>> # // BUG: "need 'this' to access member AsciiChar"
>> # // Seems like a named union error, because if it's
>> # // unnamed it works just fine, but it's a MS struct
>> # // so I need it the way it is.
>> # private import std.c.windows.windows;
>> #
>> # extern( Windows )
>> # {
>> # struct CHAR_INFO
>> # {
>> #     union Char  //<-- if Char is commented out...ok
>> #     {
>> #         WCHAR UnicodeChar;
>> #         CHAR  AsciiChar;
>> #     }
>> #
>> #     WORD Attributes;
>> # };
>> # }
>> #
>> # int main()
>> # {
>> #     CHAR_INFO chiFill;
>> #     chiFill.Char.AsciiChar = cast(char)'\x20'; //<--need 'this' to access
>> #                                                //   member AsciiChar
>> #     //chiFill.AsciiChar = cast(char)' '; //<-- works with Char commented out
>> #     return 0;
>> # }
>> 
>> Output:
>> ----------
>> C:\dmd>dmd needthis.d
>> needthis.d(26): need 'this' to access member AsciiChar
>
>
>-- 
>Tomasz Stachowiak  /+ a.k.a. h3r3tic +/

Thanks Tomasz!

BTW, your handle / alias "h3r3tic" reminds me of someone that had a similar name, who I remember made good levels / maps for "Heretic 2" back in the day.

David L.

-------------------------------------------------------------------
"Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
-------------------------------------------------------------------

MKoD: http://spottedtiger.tripod.com/D_Language/D_Main_XP.html
June 18, 2005
David L. Davis wrote:
> BTW, your handle / alias "h3r3tic" reminds me of someone that had a similar
> name, who I remember made good levels / maps for "Heretic 2" back in the day.

Not me, sorry ;) I enjoyed playing Heretic2 but I never made a map for it


-- 
Tomasz Stachowiak  /+ a.k.a. h3r3tic +/