How can I make typeof(this) return the type of a calling derrived class from a function in a base class?

2017-06-16 Thread Lester via Digitalmars-d-learn
If I have something like the following: class A { void foo(){ writeln(typeof(this)); } ... } class B : A { ... } And I want the results: A a = new A; B b = new B; a.foo(); // prints "A" b.foo(); // prints "B" How would I go about doing that? At the moment b.foo() is printing "A".

Re: How can I make typeof(this) return the type of a calling derrived class from a function in a base class?

2017-06-16 Thread Milan Suk via Digitalmars-d-learn
On Friday, 16 June 2017 at 13:46:14 UTC, Lester wrote: If I have something like the following: class A { void foo(){ writeln(typeof(this)); } ... } class B : A { ... } And I want the results: A a = new A; B b = new B; a.foo(); // prints "A" b.foo(); // prints "B" How would I go a

Re: How can I make typeof(this) return the type of a calling derrived class from a function in a base class?

2017-06-16 Thread Adam D. Ruppe via Digitalmars-d-learn
On Friday, 16 June 2017 at 13:46:14 UTC, Lester wrote: If I have something like the following: class A { void foo(){ writeln(typeof(this)); } try one of these: http://dlang.org/spec/template.html#TemplateThisParameter Though note that the this in there is still the static type at the u

Re: How can I make typeof(this) return the type of a calling derrived class from a function in a base class?

2017-06-16 Thread Lester via Digitalmars-d-learn
Thanks for the responses guys :) I ended up using a foo(this T) and it works! Thanks again for your help.