parent class get the subclass object

Brian via Digitalmars-d digitalmars-d at puremagic.com
Mon Jan 16 22:20:34 PST 2017


On Monday, 16 January 2017 at 16:47:09 UTC, Nemanja Boric wrote:
> On Monday, 16 January 2017 at 16:31:41 UTC, Brian wrote:
>> [...]
>
> You're missing a cast, since `this` is a reference to a 
> superclass.
>
> import std.stdio : writeln;
>
> abstract class Base(T)
> {
>     this()
>     {
>         _this = cast(T)(this);
>         assert(_this);
>     }
>
>     void hello()
>     {
>         _this.world();
>     }
>
>     private
>     {
>         T _this;
>     }
> }
>
>
> class Sub : Base!Sub
> {
>     void world()
>     {
>         writeln("Hello world");
>     }
> }
>
> void main()
> {
>     Sub sub = new Sub;
>     sub.hello();
> }

Yes, it's possible, thank you!


More information about the Digitalmars-d mailing list