On 2015-03-05 19:35:34 +0000, Chris Sperandio said:
Hi,
I'm a developer coming from C and I've a question about class instance
as method or function parameter.
In the book "The D Programming Language", I read the instance was
passed by reference to functions (in the opposite of structures). I
understood that it was the same object in the function and the caller.
But I'm think, I was wrong because when I print the addresses of an
object before the function call and inside the function, they're not
the same but the changes from the function are kept in the instance.
If I use the "ref" qualifier in the function declaration, the 2
addresses are the same.
How do the changes work in the function? Is there a copy ? Or a "magic"
trick :) ?
Chris
When you write `auto myObject = new MyObject();`
`myObject` is actually a pointer to object in GC memory. Its roughly
equivalent to `struct MyObject *myobject` in C. So when you take a
pointer you actually take a pointer to reference on the stack and thats
why its different in the function - variable is higher up the stack.
`ref` qualifyer guaranties that you get the pointer to the same reference.
If you really need the actual pointer to object data you can use
`*cast(void**)&myObject`. Compiler cannot cast object reference to
`void*` but we can trick it ;)