D Classes Passed By Reference or Value?

Brandon Ragland via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Aug 16 15:31:01 PDT 2015


Hi All, I'm a bit confused as to how Classes in D are passed in 
arguments and returns.

Take this for example:

class MyClass{
int x = 2;
}

And then in app.d

ref MyClass doStuff(){
MyClass mc = new MyClass() // Heap allocation, using new....
return mc;
}

The above fails, as "escaping reference to local variable" 
however, this was created using new.... Not understanding what 
the deal is, this should be a valid heap allocated object, and 
therefore, why can I not pass this by reference? I don't want 
this to be a local variable...

So this begs the question: Are Classes (Objects) passed by 
reference already?

-Brandon


More information about the Digitalmars-d-learn mailing list