Inner struct accessing host member
Artur Skawina via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Aug 5 16:46:28 PDT 2014
On 08/05/14 22:32, Philippe Sigaud via Digitalmars-d-learn wrote:
> I'd have thought that this would work:
>
> struct A
> {
> int[] i;
> B b;
>
> struct B
> {
> void foo() { i ~= 1;}
> }
> }
>
> void main()
> {
> A a;
> a.b.foo();
> }
>
> But the compiler tells me 'need this for i of type int[]'.
> Is there any way I can gain access on i inside B?
Not directly, but as you ask for /any/ way -- yes:
struct B
{
void foo() { outer.i ~= 1; }
ref A outer() inout @property { return *cast(A*)(cast(void*)&this-A.b.offsetof); }
}
Note this will work only as long as you have just one B
instance in A and B is never created or copied outside of A.
artur
More information about the Digitalmars-d-learn
mailing list