Class field inheritance

Simen kjaeraas simen.kjaras at gmail.com
Wed Jun 30 13:33:27 PDT 2010


André Wagner <andre.nho at gmail.com> wrote:

> Ok, bad example :)
>
> Here's a better example of what I wanted to ask:
>
> class XA { uint x; }
> class XB : XA { uint y; }
>
> class A
> {
>     XA j;
>     this(XA j) { this.j = j; }
> }
>
> class B : A
> {
>     XB j;
>     this(XB j) { super(j); }
> }
>
> void main()
> {
>     XB j = new XB();
>     j.x = 10;
>     j.y = 20;
>     B b = new B(j);
>     writefln("%d\n", b.j.x);
> }
>
> Why doesn't this work?

That would be because B.j hides A.j. A's constructor sets
B.super.j, not B.j.

If you try in your main, writeln( cast( A )( b ).j.x );, you
will see that A.j is set correctly.

-- 
Simen


More information about the Digitalmars-d mailing list