Re: Newbie question: Return a locally allocated variable

2020-09-14 Thread Fitz via Digitalmars-d-learn
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 `B

Re: Newbie question: Return a locally allocated variable

2020-09-14 Thread Adam D. Ruppe via Digitalmars-d-learn
On Monday, 14 September 2020 at 16:29:11 UTC, Fitz wrote: I expect the following code below to create 10 items with 10 different addresses, instead they all have the same address? You are taking the address of the local variable holding reference, not the reference itself. class Bob { } Bo

Newbie question: Return a locally allocated variable

2020-09-14 Thread Fitz via Digitalmars-d-learn
I expect the following code below to create 10 items with 10 different addresses, instead they all have the same address? import std.stdio; class Bob { } void main() { for (auto i = 0; i < 10; i++) { auto pBob = bobFactory(); writefln("bob @ %x\n", pBob); } } Bob *bobF