What does the syntax *this = {}; do in a class function?

Adam D. Ruppe destructionator at gmail.com
Wed Sep 18 18:16:14 UTC 2019


On Wednesday, 18 September 2019 at 17:58:08 UTC, GilbertJobs 
wrote:
> Specifically, I saw this in a reset function for a class - what 
> exactly does this "reset"?

Are you sure it was a class, or was it a struct? I don't think 
that'd compile in a D class, but in a D struct - at least if this 
was old code*, the rules have changed now - it'd look like 
setting it to default values.

Struct S = {}; // the {} there is a struct initializer

you can do like S = { name: value }, and leaving nothing in i 
believe is the default values from the definition, e.g.

struct S {
    int a = 10;
    int b;
}

there a is set to 10 it not specified and b is set to 0 (the 
default for type int).

so i suspect it is doing that.


* If I was writing this in new code, it would probably just look 
like `this = typeof(this).init;`


More information about the Digitalmars-d mailing list