Is there any way to create something like this?

RenatoUtsch renatoutsch at gmail.com
Sun Oct 21 06:10:15 PDT 2012


Hello,

I was trying to imagine how to do a thing such the one as below 
(or similar), because it would be great for the project I am 
creating...

Is there any way to create some kind of object that resembles 
different objects (like a superclass with the subclasses, but 
with transparent access to the subclasses new methods)?

For example:

[code]
void main() {
     GeneralLibrary library = createLibrary("1.0");
     GeneralLibrary library11 = createLibrary("1.1");
     GeneralLibrary library12 = createLibrary("1.2");
     GeneralLibrary library2 = createLibrary("2.0");

     library.func1(); // Should work
     library.func11(); // Should fail
     library.func12(); // Should fail
     library3.func2(); // Should fail

     library2.func1(); // Should work
     library2.func11(); // Should work
     library21.func12(); // Should fail
     library3.func2(); // Should fail

     library2.func1(); // Should work
     library2.func11(); // Should work
     library2.func12(); // Should work
     library3.func2(); // Should fail

     library3.func1(); // Should fail
     library3.func11(); // Should fail
     library3.func12(); // Should fail
     library3.func2(); // Should work
}

class Library_1_0 {
     void func1() {}
}

class Library_1_1 : Library_1_0 {
     void func11() {}
}

class Library_1_2 : Library_1_1 {
     void func12() {}
}

class Library_2_0 {
     void func2() {}
}
[/code]

Is there any way to make the GeneralLibrary class and the 
createLibrary() (or even better, make the GeneralLibrary 
constructor do that) to work with this kind of construction?


More information about the Digitalmars-d mailing list