Anonymous structure

Tofu Ninja via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Apr 17 20:23:52 PDT 2016


On Monday, 18 April 2016 at 02:42:15 UTC, Nicholas Wilson wrote:
> On Monday, 18 April 2016 at 02:12:24 UTC, Tofu Ninja wrote:
>> Just out of curiosity, what is the point of the following?
>>
>> struct a{
>> 	struct{
>> 		int x;
>> 		int y;
>> 		int z;
>> 	}
>> }
>>
>> As far as I can tell, the anonymous structure does nothing. 
>> How is it different from
>>
>> struct a{
>>
>> 	int x;
>> 	int y;
>> 	int z;
>> }
>>
>
> IIRC D doesn't allow anonymous structures.
>


It does, it compiles...
Accessing x,y,z on the first one with the anonymous struct is the 
same as accessing it on the second without the anonymous 
struct... Seems to make no difference that it is there, which is 
why I am asking.

>> Also is there a way to have a named substructure, not a nested 
>> structure but something to just add an additional name, maybe 
>> something like
>> struct a{
>> 	struct{
>> 		int x;
>> 		int y;
>> 		int z;
>> 	} b;
>> }
>
> Try adding static:
> struct a
> {
>     static struct b
>     {
>     }
> }

Does not seem to be what I mean, a static nested struct is just a 
nested struct without access to the enclosing structure's 
members. What I meant was a struct to just add a namespace of 
sorts to the struct so that the substructure members would have 
to be accessed with a longer more qualified name. Something like

struct a{
      int x;
      sub_struct b{
            int y;
      }
}

a v;
v.x = 3;
v.b.y = 7;
// v.y = 7; // does not work


More information about the Digitalmars-d-learn mailing list