~= call copy ctor?

Namespace rswhite4 at googlemail.com
Fri Jul 20 06:50:19 PDT 2012


New question:

I have this code:
[code]
import std.stdio;

struct Test {
public:
	this(int i = 0) {
		writeln("CTOR");
	}

	this(this) {
		writeln("COPY CTOR");
	}

	~this() {
		writeln("DTOR");
	}
}

void main() {
	Test[] _arr;

	_arr ~= Test(0);

	writeln("end main");
}
[/code]

And as output i see:

CTOR
COPY CTOR
DTOR
end main

Why on earth....?

I create a struct Test. It's not a local variable, it's directly 
assigned,
but it is copied and the original is destroyed. Why?
If i store something important, like a pointer, in Test and will 
free him in the DTOR i cannot assign this way Test's to an array, 
because the pointer was deleted because the DTOr was called.

I think the correct output would be:

CTOR
end main

Maybe DTOR before "end main". But not COPY CTOR anywhere.


More information about the Digitalmars-d-learn mailing list