Struct inheritance

ketmar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Feb 24 10:24:35 PST 2015


On Tue, 24 Feb 2015 18:19:39 +0000, ketmar wrote:

> On Tue, 24 Feb 2015 12:05:50 +0000, amber wrote:
> 
>> Hi,
>> 
>> Is it possible in D to have inheritance using value types, i.e.
>> structs?
>> 
>> 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?
> 
> there is no "copy constructor" in D. structs are copied by `memcpy()`,
> and then compiler calls "postblit" aka `this(this)`. it may seem like
> constructor, but it's not. it's a "fixup", that fixes inconsistencies
> that are left by `memcpy()`. i.e. when postblit is called, structure is
> already copied.
> 
> and `opAssign` is... well, overloading of "=" operator. it has to
> manually create a structure copy and manually copy everything you need.
> and it will never be called by compiler "behind the scenes", like
> postblit.

oops. of course, you already has a struct for `opAssign` -- it need 
`this` after all! ;-) but no automatic copying. it's just a function 
call, nothing magic here.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20150224/f4274faf/attachment.sig>


More information about the Digitalmars-d-learn mailing list