Forward reference in struct

Simen Haugen simen at norstat.no
Mon Oct 8 07:01:45 PDT 2007


"Frits van Bommel" <fvbommel at REMwOVExCAPSs.nl> wrote in message 
news:fed9j3$udv$1 at digitalmars.com...
> Let's disregard the int. You essentially have this:
> -----
> struct A {
>     B b;
> }
> struct B {
>     A a;
> }
> -----
> Now ask yourself this question: How big is A?
> Obviously, since it's a struct with 1 member, it's equal to the size of B. 
> But how big is B? By the same reasoning, it's as big as A.
> You have an infinite "member recursion", which isn't allowed.
>
> To fix this, you may want to change A.b and/or B.a to a pointer, or change 
> A and/or B from a struct to a class (since classes are always stored by 
> reference, which is essentially an implicit pointer).

Of course... I changed one to use pointers instead and it fixed the problem.
Thanks to both Frits and Regan.




More information about the Digitalmars-d-learn mailing list