Accessing outer class attribute from inner struct

Moritz Maxeiner via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Aug 28 15:28:18 PDT 2017


On Monday, 28 August 2017 at 21:52:58 UTC, Andre Pany wrote:
> [...]
>
> To make my question short:) If ColumnsArray is a class I can 
> access the attribute "reference" but not if it is a struct. I 
> would rather prefer a struct, but with a struct
> it seems I cannot access "reference".
>
> How can I access "reference" from my inner struct?
>
> [...]

Add an explicit class reference member to to it:
---
class TCustomGrid: TCustomPresentedScrollBox
{
	struct ColumnsArray
	{
		TCustomGrid parent;

		TColumn opIndex(int index)
		{
			int r = getIntegerIndexedPropertyReference(reference, 
"Columns", index);
			return new TColumn(r);
		}
	}
	
	ColumnsArray Columns;

	this()
	{
		Columns = ColumnsArray(this);
	}
...
}
---

Nesting structs inside anything other than functions[1] is for 
visibility/protection encapsulation and namespacing only.

[1] non-static structs in functions are special as they have 
access to the surrounding stack frame


More information about the Digitalmars-d-learn mailing list