D Cookbook range save question

Adam D. Ruppe destructionator at gmail.com
Thu Jan 30 13:41:21 UTC 2020


(I'll answer here anyway just in case someone lands here via a 
web search.)

On Thursday, 30 January 2020 at 10:31:08 UTC, mark wrote:
> Why is this a *copy*? (For a copy (in C++) I'd have expected 
> return *this.)

In C++, `this` is a pointer, but in D, it is a reference. So 
assignment follows those semantics instead; `Foo a = this` does a 
copy assignment and if you want a pointer, you need to explicitly 
ask for one with the & operator: `Foo* a = &this;`

fun fact, old versions of D, ~11ish years ago, actually worked 
the same way as C++. But the reference one was generally nicer 
and you can turn it back to the pointer as needed so it got 
changed.


More information about the Digitalmars-d-learn mailing list