Newbie question: Return a locally allocated variable

Fitz fitz at
Mon Sep 14 16:53:20 UTC 2020

On Monday, 14 September 2020 at 16:44:14 UTC, Adam D. Ruppe wrote:

> This is a common mistake with people coming from C++. A D class 
> is more like a Java class - it is automatically a reference.
> So your class Bob here in D would actually be represented as 
> `Bob*` in C++.
> Thus when you define `Bob*` in D, that's like a `Bob**` in 
> C++... a pointer to a pointer. Thus you're getting the address 
> on the stack of the local, not at all what you want.
> Your factory should really just be:
> Bob bobFactory() { return new Bob; }
> Then to compare addresses, do:
> writeln(cast(void*) bob); // convert the reference itself to a 
> pointer

thank you! I was driving myself mad.
makes sense now.

More information about the Digitalmars-d-learn mailing list