Programming in D p288. Destructor called too many times.

Nick Treleaven nick at geany.org
Wed Feb 11 21:30:03 UTC 2026


On Wednesday, 11 February 2026 at 12:41:50 UTC, Brother Bill 
wrote:
> On Tuesday, 10 February 2026 at 20:37:04 UTC, Nick Treleaven 
> wrote:
>> If you add a copy constructor to Duration, you can see when it 
>> gets copied:
> ```
>> 	this(ref Duration d)
>> 	{
>> 		this.tupleof = d.tupleof;
>> 		writeln("copying Duration: ", toString);
>> 	}
> ```
>
> The ```ref``` is needed to avoid yet another copy.

It's more that `ref` is part of what signifies a copy constructor:
https://dlang.org/spec/struct.html#struct-copy-constructor

> Please explain what ```this.tupleof = d.tupleof;``` does in 
> detail.

It copies the fields of `d` to the fields of `this`.

https://dlang.org/spec/class.html#tupleof


More information about the Digitalmars-d-learn mailing list