anonymous structs within structs

Jonathan M Davis newsgroup.d at jmdavisprog.com
Tue Dec 5 16:32:59 UTC 2023


On Monday, December 4, 2023 11:26:07 AM MST DLearner via Digitalmars-d-learn 
wrote:
> Suppose we need a construct like:
> ```
> void main() {
>
>     struct A {
>        int I1;
>        int I2;
>        char X;
>     }
>
>     struct B {
>        A Dummy;
>        int Var1;
>        int Var2;
>     }
> }
> ```
> But do not want to give an explicit name (like 'Dummy' above) to
> the A struct held within the B struct.
>
> Just removing 'Dummy' does not work (Error: no identifier for
> declarator `A`).
> Nor does replacing 'Dummy' with {}
>
> Suggestions?

Normally, if you're not going to actually use the member variables, then
there's no point in them even being there. However, if you need them there
for alignment purposes, then you can just make them private.

- Jonathan M Davis





More information about the Digitalmars-d-learn mailing list