Passing struct to function

Michał mmcomando at gmail.com
Wed Jun 13 14:43:24 UTC 2018


When I pass my struct to function something is going wrong. I 
don't know how to fix it.

Code:
import std.stdio;


void print(ref Vector v, string s){
		writefln("%s==%s    %s", &v.x, v.ptr, s);
}

struct Vector {
	int x;
	int* ptr;

	this(this) {
		ptr = &x;
		print(this, "postblit");
	}
}

void someFunc(Vector t) {
	print(t, "in someFunc");
}

void main() {
	auto tmpA = Vector();
	tmpA.ptr = &tmpA.x;
	print(tmpA, "start");

	someFunc(tmpA);
}


Result on my machine:
7FFF7D70BC00==7FFF7D70BC00    start
7FFF7D70BBF0==7FFF7D70BBF0    postblit
7FFF7D70BBD0==7FFF7D70BBF0    in someFunc

In the last line pointers are not matching. I thought that 
postblit will do the thing but it is not the case. How to make 
'ptr' to be null or '&this.x' all the time?


More information about the Digitalmars-d-learn mailing list