Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing to outer class

Tags:

class

d

I am reading "The D Programming Language" book at the moment. It is telling about the inner classes.

class Outer{
    int x;

    this(){
        x = 99;
        new Inner;
    }

    class Inner{
        int x;

        this(){
            x = 5;
            writeln( "Inner x = ", x, "; Outer x = ", this.outer.x );
        }
    }
}

As the book tells, to be able to access the class Outer's x, I need to use this.outer.x. But one confusing thing is the name of class "Outer" turns into lower case "outer". With normal class name "Outer", compiler gives error message. It is like compiler decides what you need to use as name and forces to use what name it generates.

My problem is, when I rename the class "Outer" to "bLaH", now I am not able to outer class any more.

class bLaH{
    int x;

    this(){
        x = 99;
        new Inner;
    }

    class Inner{
        int x;

        this(){
            x = 5;
            writeln( "Inner x = ", x, "; Outer x = ", this.bLaH.x );
        }
    }
}

It is not obvious what name to use while accessing to outer class. The first letter is lower case now, but compiler says "test1.bLaH" is not defined. (File name is test1.d)

If I convert "this.bLaH" to "this.blah" by thinking that maybe compiler converts outer class' name to all lower case, this time, compiler says "undefined variable 'blah'".

Is there a design error in the language about this? As a programmer, I don't like this type of name conversion done by compiler.

I am on Windows XP, DMD version is 2.060.

like image 306
tcak Avatar asked Jun 16 '26 08:06

tcak


1 Answers

outer is a keyword. It’s like super or whatever. A class is always nested in only one class, so the outer keyword refers to that class.

http://dlang.org/class.html#nested

Here you can find that outer is actually a property.

like image 116
phaazon Avatar answered Jun 18 '26 00:06

phaazon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!