Copy Constructor
Salih Dincer
salihdb at hotmail.com
Mon Jun 6 23:18:55 UTC 2022
On Sunday, 5 June 2022 at 15:45:17 UTC, Salih Dincer wrote:
> Also, when we write to the screen with writeln(), why four
> times copy-constructors are running?
>
> **Playground:** https://run.dlang.io/is/qHvLJe
I solved the problem by implementing the `toString()` member
function. I also had to use a helper `writeout()` as below:
**struct Foo {**
```d
string toString() {
return format("%s", payload);
}
```
**void main() {**
```d
void writeout(T)(T text) {
text.toString.writeln;/*
import std.conv;
text.to!string.writeln;//*/
}
writeout(three);
```
Interestingly, even using `to!string` the copy-constructor works
extra +4 times! I think there will be performance losses if
`write()` is used unconsciously everywhere! Although this way, it
kisses with ctor +1 times :)
SDB at 79
More information about the Digitalmars-d-learn
mailing list