Q: How to return sub class from base class method

Myron Alexander someone at somewhere.com
Sun Jun 17 17:38:29 PDT 2007


Hello.

I have a class structure as such:

> class A {
>    typeof(this) doSomething (????) {
>       ...
>       return this;
>    }
> }
> 
> class B : A {
>    typeof(this) doSomethingElse (????) {
>       ...
>       return this;
>    }
> }
> 
> void main () {
>    // Fails
>    B b = (new B()).doSomething (???).doSomethingElse (???);
> }

The method chain fails as doSomething returns type A.

I want to define method doSomething in such a way that it will return 
the specialized type (B) rather than the base type.

I'm currently using a mixin to mixin overriding code:

> template doSomethingOverride () {
>    override typeof(this) doSomething (????) {
>       super.doSomething (????);
>       return this;
>    }
> }

Is there a way to set the return type as the type instantiated?

Thanks,

Myron.


More information about the Digitalmars-d-learn mailing list