Struct dtor on ref variable

Patric via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Aug 1 09:01:17 PDT 2016


struct Test{
	int x;
	this(int v){
		x = v;
		writeln(x.to!string ~ " Created");
	}
	~this(){
		writeln(x.to!string ~ " Destroyed");
	}
	void opOpAssign(string op, Type)(ref Type s){
		x = s.x;
	}
}
void showme(Type)(ref Type t){
	writeln(t.x);
}
void main(){
	auto t = Test(1);
	auto t2 = Test(2);
	showme(t);
	showme(t2);
	t = t2;
}

Prints:
1 Created
2 Created
1
2
1 Destroyed
2 Destroyed
2 Destroyed

this line:
t = t2
Causes the dtor to be called.

Why?
I expected nothing to happen because "ref" its a simple pointer, 
right?
Or I am missing something here?



More information about the Digitalmars-d-learn mailing list