Thread overview | ||||||
---|---|---|---|---|---|---|
|
December 23, 2012 new or no? | ||||
---|---|---|---|---|
| ||||
for(uint y=0; y<10; ++y){ auto m = new Mutex(); writeln(&m); } run: 7F66E5A05CF8 7F66E5A05CF8 7F66E5A05CF8 7F66E5A05CF8 7F66E5A05CF8 7F66E5A05CF8 7F66E5A05CF8 7F66E5A05CF8 7F66E5A05CF8 7F66E5A05CF8 As I understand it, all the time return one object But: auto m = new Mutex(); writeln(&m); auto m1 = new Mutex(); writeln(&m1); run: 7FCF60698CF0 7FCF60698CF8 |
December 23, 2012 Re: new or no? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Shadow_exe | On Sunday, 23 December 2012 at 19:43:54 UTC, Shadow_exe wrote:
> for(uint y=0; y<10; ++y){
> auto m = new Mutex();
> writeln(&m);
> }
That's the address of the local variable. The object it points to is somewhere else.
An Object in D is more like an Object* in C++.
Object* o = new Object();
&o == 0
o == 1
o = new Object();
&o == 0 // the local variable is still in the same place
o == 2 // but it now points to a new object
|
December 23, 2012 Re: new or no? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Shadow_exe | On 12/23/12, Shadow_exe <shadow_exe@ukr.net> wrote:
> for(uint y=0; y<10; ++y){
> auto m = new Mutex();
> writeln(&m);
> }
Use writeln(cast(void*)m) to get the address of the object, otherwise
you're writing the address of the reference (which each time refers to
a different object).
|
December 23, 2012 Re: new or no? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | Yes, thank you! Tired apparently, I need to rest... |
Copyright © 1999-2021 by the D Language Foundation