Copy instead of reference?

Namespace rswhite4 at googlemail.com
Thu May 23 02:47:08 PDT 2013


I get this output:

====

----
CTor 42
DTor 0
Return A
Postblit 42
----
DTor 84
DTor 42

====

with the following code. I'm a bit confused about the Postblit. I 
return by ref so I thought that I get a const ref of the original 
A.

[code]
import std.stdio;

struct A {
public:
	int id;

	this(int id) {
		writeln("CTor ", id);

		this.id = id;
	}

	this(this) {
		writeln("Postblit ", this.id);

		this.id *= 2;
	}

	~this() {
		writeln("DTor ", this.id);
	}
}

class B {
private:
	A _a = void;

public:
	this() {
		this._a = A(42);
	}

	ref const(A) getA() const {
		writeln("Return A");
		return this._a;
	}
}

void main() {
	writeln("----");
	B b = new B();
	A a = b.getA();
	writeln("----");
}
[/code]


More information about the Digitalmars-d-learn mailing list