I do not understand copy constructors

Tejas notrealemail at gmail.com
Thu Aug 12 12:49:18 UTC 2021


On Thursday, 12 August 2021 at 12:28:32 UTC, Learner wrote:
> On Thursday, 12 August 2021 at 12:22:22 UTC, Tejas wrote:
>> On Thursday, 12 August 2021 at 12:19:56 UTC, Tejas wrote:
>>> [...]
>>
>> Works with ```@safe``` as well
>>
>> Paul was just trying to make that other answer work, you don't 
>> have to make copy constructors ```@trusted```
>
> Operations are needed on `other` data, that was the reason for 
> a `postblit` in the original case: an `int[]` data array needs 
> to be duplicated.

Hey, this should be good enough now:
```d
import std;
struct Foo {
  	this(ref inout Foo other) /*inout*/ @safe{
  		/*foreach(i, v; other.tupleof)
  			this.tupleof[i] = cast(typeof(this.tupleof[i]))v;*/
         a = other.a;
         b = other.b;
         foreach(i, elem ;other.c)
             c[i] = elem;

  	}

  	@disable this(this);
     int a;
     float b;
     double[] c;
  }

void main()@safe{
     immutable Foo a;
     const Foo c;
     Foo b = a;//mutable b from immutable a
     //writeln(typeof(b).stringof);  //Output is Foo
     const Foo d = c;//const d from const c
     Foo e = c;//mutable e from const c
     //immutable Foo f = b;//immutable f from mutable b     I 
don't know why this fails but const from mutable succeeds
     const Foo g = b;//const g from mutable b
}
```


More information about the Digitalmars-d-learn mailing list