Struct inheritance

amber via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Feb 24 04:44:44 PST 2015


On Tuesday, 24 February 2015 at 12:16:43 UTC, Tobias Pankrath 
wrote:
> On Tuesday, 24 February 2015 at 12:05:51 UTC, amber wrote:
>> Hi,
>>
>> Is it possible in D to have inheritance using value types, 
>> i.e. structs?
>
> No runtime polymorphism, but a kind of sub typing via alias 
> this.
>
> struct S { void foo() { writeln("S.foo"); }
> struct T { S s; alias s this; }
>
> T t;
> t.foo(); // prints S.foo
>
>
>> Also I don't quite understand how copy ctors work in D. Do I 
>> need to implement opAssign(S other) {}, or this(this) {} and 
>> what's the difference between these two?
>
> If available, opAssign will be used in an assignment like x = y;
> You're custom opAssign can take arbitrary parameter types, so 
> typeof(y) does not have to be typeof(x).
> 
> postblit is used for copy construction. This could be 
> assignment if no opAssign is provided (not sure about this), 
> but also e.g. passing parameter by value or returning from a 
> functions

Thank you Tobias I get it now.

/amber


More information about the Digitalmars-d-learn mailing list