parent class get the subclass object

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


On Monday, 16 January 2017 at 19:31:50 UTC, Mike Parker wrote:
> On Monday, 16 January 2017 at 16:31:41 UTC, Brian wrote:
>
>> No, you don't understand I want to express meaning.
>>
>> other programing language is allow this.
>>
>> Your code more like the old C++.
>>
>> If a high-level programing language or need haevy like C++ 
>> impl code, It's very regret.
>>
>> Can like rust / swift / php / java / C# ?
>
> Perhaps template this parameters [1]:
>
> ```
> class Base {
>     void hello(this C)() {
>         (cast(C)this).world();
>     }
> }
>
> class Sub:Base {
>     void world() { writeln("Hello world"); }
> }
>
> void main() {
>     auto sub = new Sub;
>     sub.hello();
> }
> ```
>
> [1] http://dlang.org/spec/template.html#TemplateThisParameter

Thanks, I think this is the best way!


More information about the Digitalmars-d mailing list