Static method of inner class needs this
Steven Schveighoffer via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Feb 9 10:33:25 PST 2015
On 2/9/15 4:30 AM, ketmar wrote:
> On Mon, 09 Feb 2015 07:32:32 +0000, rumbu wrote:
>
>> class Outer {
>> class Inner {
>> static Inner createInner()
>> {
>> return new Inner(); //need 'this' to access member
>> this
>> }
>> }
>> }
>>
>> Is this a bug?
>
> strictly speaking, this is not a bug. compiler doesn't do deep analysis
> on nested structures/classes to determine if they really require context
> pointer. you can use `static class Inner` to tell the compiler that
> `Inner` doesn't require any context.
>
To expand on this, nested classes (that is, a class nested inside
another class) REQUIRE a pointer to the outer class instance (accessed
via hidden member outer). The reason for this is Java portability.
Seriously :)
-Steve
More information about the Digitalmars-d-learn
mailing list