Error in assignment to 'this' inside constructor

evilrat evilrat666 at gmail.com
Sun May 12 04:27:45 PDT 2013


oh right,
struct A {
this(this) { copy referenced mem here}
}

or
struct A {
ref A opAssign(ref const A other) { this = other; return this; }
}

so if you have fields that should be fully copied like array this 
is the place to do it, otherwise there is a reference arguments, 
if both is no go for you then this behavior would make more 
trouble than benefits and thats.

so gather all up here are some little example
----

struct A
{	
	int x;
	int[] array;
	
	this(int x) { this.x = x; array ~= x; }
	this(this) { array = array.dup; }
	
	ref A opAssign(ref const A other) { this = other; return this; }
}

void main()
{
A a1 = A(1);
A a2 = A(2);
A a3 = a2;

a2.x = 5;

assert(a2.x == 5);
assert(a2.array[0] == 5);

assert(a3.x == 2);
assert(a3.array[0] == 2);

assert(a2.array.length == 1);
assert(a3.array.length == 1);

}


More information about the Digitalmars-d-learn mailing list