Modify Object Pointer during Initialzation

Adam D. Ruppe destructionator at gmail.com
Tue Apr 8 07:13:09 PDT 2014


On Tuesday, 8 April 2014 at 14:04:01 UTC, Jeroen Bollen wrote:
> Basically, why is this its own variable? Why doesn't D simply 
> use the variable it was called with?

A class reference is basically a pointer, passing it by reference 
to each method would be a double pointer and wasted effort most 
the time; all most methods care about is where to get the object 
data, they don't need to know how the caller was getting to the 
object data.

> I don't see why this needs to be a new variable and cannot 
> simply be 'passed-by-reference'.

You can get that with UFCS btw:

class A {}

void f(ref A a) { /* modify a here and it will change */ }

void main() {
   A a = new A;
   a.f; // a is passed by reference
}


More information about the Digitalmars-d-learn mailing list