Subclass Type

Mason Green mason.green at gmail.com
Fri Aug 15 05:41:48 PDT 2008


Damn, my code was cut off.... here it is again...

class FooBar {

    int key;
    
    this() {
    }
    
    this(int key) {
        this.key = key;
    }
    
    void printKey() {
        Stdout(key).newline;
    }    
}

class Foo : FooBar {
       
    this() {
        super(1);
    }
    
    override void printKey() {
        Stdout(key).newline;
    }    
    
}

class Bar : FooBar {

    this() {
        super(2);
    }
    
    override void printKey() {
        Stdout(key).newline;
    }
}

void main() {

    FooBar x = new Foo();
    FooBar y = new typeof(x);
    
    x.key++;
    y.printKey(); // This prints 0. I want it to print 1
    
}



More information about the Digitalmars-d-learn mailing list