Struct inside a class: How to get outer?
A Guy With a Question
aguywithanquestion at gmail.com
Mon Dec 4 16:29:33 UTC 2017
On Monday, 4 December 2017 at 14:01:08 UTC, Steven Schveighoffer
wrote:
> On 12/3/17 2:38 AM, Jonathan M Davis wrote:
>> On Sunday, December 03, 2017 01:05:00 Nick Sabalausky via
>> Digitalmars-d-
>> learn wrote:
>>> Is this even possible? My attempts:
>>>
>>> class Outer {
>>> struct Inner {
>>> void foo() {
>>> // Error: no property 'outer' for type 'Inner'
>>> Outer o = this.outer;
>>>
>>> // Error: cannot implicitly convert expression
>>> // this of type Inner to testNested.Outer
>>> Outer o = this;
>>> }
>>> }
>>> }
>>
>> As I understand it, there is no outer for nested structs, only
>> nested
>> classes. So, you'll either have to use a nested class or
>> explicitly pass a
>> reference to the outer class to the nested struct.
>
> Yes, for structs inside structs or classes, there is no 'outer'
> member.
>
> However, there is a hidden context member in a struct if it's
> nested inside a function. In that case, you must label the
> struct "static"
>
> The only reason inner classes have outer pointers to their
> "owner" class instance, is for those familiar with Java
> programming style (specifically, IIRC, it was to write dwt,
> which was a port of jwt). I believe Walter mentioned elsewhere
> recently, he would have done things differently today.
>
> -Steve
Yes! Don't forget the 'static' otherwise you'll get some odd
errors.
More information about the Digitalmars-d-learn
mailing list