this(this) / opAssign

Namespace rswhite4 at googlemail.com
Thu Jan 10 15:03:29 PST 2013


Is it expected behaviour that this code compiles and prints:

this(this)
this(this)

?
[code]
import std.stdio;

static if (!is(typeof(writeln)))
	alias writefln writeln;

struct vec2f {
public:
	float x, y;
	
	this(this) {
		writeln("this(this)");
	}
}

void main()
{
	vec2f v1 = vec2f(0, 1);
	vec2f v2 = vec2f(2, 3);
	
	v2 = v1;
	
	vec2f v3 = v2;
}
[/code]

while this code

[code]
import std.stdio;

static if (!is(typeof(writeln)))
	alias writefln writeln;

struct vec2f {
public:
	float x, y;
	
	this(this) {
		writeln("this(this)");
	}
	
	
	ref vec2f opAssign(ref const vec2f v) {
		writeln("opAssign");
		
		this.x = v.x;
		this.y = v.y;
		
		return this;
	}
}

void main()
{
	vec2f v1 = vec2f(0, 1);
	vec2f v2 = vec2f(2, 3);
	
	v2 = v1;
	
	vec2f v3 = v2;
}
[/code]

prints:

opAssign
this(this)
?

And what should I use, this(this) or opAssign? I thougth that it 
is unimportant,
but that changed my mind: http://dpaste.dzfl.pl/36ce3688


More information about the Digitalmars-d-learn mailing list