Class member always has the same address

2016-03-19 Thread szymski via Digitalmars-d-learn
Hello! I'm having a big problem with class members. I'm kinda new to D, so this may be my fault, but look at the following code: import std.stdio; class B { int variable; } class A { B b = new B(); } void main() { // Create 10 instances of A foreach(i; 0 ..

Re: Class member always has the same address

2016-03-19 Thread ag0aep6g via Digitalmars-d-learn
On 19.03.2016 21:24, szymski wrote: In my opinion &a.b.variable should give different addresses for each instance of A, because it's not static. What am I doing wrong? Thanks in advance. The As are different, but they all reference the same B. Initialize b in a constructor instead.

Re: Class member always has the same address

2016-03-19 Thread Mike Parker via Digitalmars-d-learn
On Saturday, 19 March 2016 at 20:24:15 UTC, szymski wrote: class A { B b = new B(); } This is *default* initialization, not per instance initialization. The compiler will create one instance of B and it will become the default initializer of b in *every* instance of A. You can ver

Re: Class member always has the same address

2016-03-20 Thread szymski via Digitalmars-d-learn
On Sunday, 20 March 2016 at 02:21:51 UTC, Mike Parker wrote: On Saturday, 19 March 2016 at 20:24:15 UTC, szymski wrote: class A { B b = new B(); } This is *default* initialization, not per instance initialization. The compiler will create one instance of B and it will become the d

Re: Class member always has the same address

2016-03-20 Thread Mike Parker via Digitalmars-d-learn
On Sunday, 20 March 2016 at 09:53:07 UTC, szymski wrote: Ok, I understand now, thanks. I used C# a lot before and there default initialization worked like per instance initialization. Yes, I assumed you were thinking of C# or Java classes with this. When coming to a new language, it's natural