Q: How to return sub class from base class method
Myron Alexander
someone at somewhere.com
Sun Jun 17 19:28:06 PDT 2007
Jari-Matti Mäkelä wrote:
>
> Yes, you can use CRTP
> (http://en.wikipedia.org/wiki/Curiously_Recurring_Template_Pattern).
>
> class A(T) {
> T doSomething() {
> ...
> return cast(T)this;
> }
> }
>
> class B(T) : A!(T) {
> T doSomethingElse() {
> ...
> return cast(T)this;
> }
> }
>
> There might be other (more clever) ways to do this too. Like a mixin for
> the "selftype" or something.
Thanks for the suggestion. I am hoping for a solution that does not
require me to set a type on instantiation. I just want to write:
B b = (new B()).doSomething (???).doSomethingElse (???);
If the language does not have a way to do it, that's ok, the mixin
solution works.
Regards,
Myron.
More information about the Digitalmars-d-learn
mailing list