digitalmars.D.learn - Get address of object in constructor.
- MGW (20/20) Mar 13 2016 I want to get address of object Cfoo in constructor. Whether it
- WebFreak001 (11/13) Mar 13 2016 class Cfoo {
- MGW (2/15) Mar 13 2016 Super! Ok!
- WebFreak001 (5/6) Mar 13 2016 However note that this is not the same as that function.
- MGW (3/7) Mar 13 2016 void* thisAddr = cast(void*) &this;
- Adam D. Ruppe (5/6) Mar 13 2016 This doesn't really make sense anyway, this is a local variable,
I want to get address of object Cfoo in constructor. Whether it is possible? now: ------------- class Cfoo { void* adrThis; void saveThis(void* adr) { adrThis = adr; } } ... Cfoo foo = new Cfoo(); foo.saveThis(&foo); shall be -------------- class Cfoo { void* adrThis; this() { adrThis = ????????? } } ... Cfoo foo = new Cfoo();
Mar 13 2016
On Sunday, 13 March 2016 at 15:43:02 UTC, MGW wrote:I want to get address of object Cfoo in constructor. Whether it is possible?class Cfoo { void* adrThis; this() { adrThis = cast(void*) this; } } ... Cfoo foo = new Cfoo(); "this" should work (pun intended)
Mar 13 2016
On Sunday, 13 March 2016 at 15:49:20 UTC, WebFreak001 wrote:On Sunday, 13 March 2016 at 15:43:02 UTC, MGW wrote:Super! Ok!I want to get address of object Cfoo in constructor. Whether it is possible?class Cfoo { void* adrThis; this() { adrThis = cast(void*) this; } } ... Cfoo foo = new Cfoo(); "this" should work (pun intended)
Mar 13 2016
On Sunday, 13 March 2016 at 15:43:02 UTC, MGW wrote:Cfoo foo = new Cfoo(); foo.saveThis(&foo);However note that this is not the same as that function. cast(void*)this and &this are 2 different things. So if you want to do the same as saveThis just do void* thisAddr = cast(void*) &this; instead
Mar 13 2016
On Sunday, 13 March 2016 at 16:02:07 UTC, WebFreak001 wrote:However note that this is not the same as that function. cast(void*)this and &this are 2 different things. So if you want to do the same as saveThis just do void* thisAddr = cast(void*) &this; insteadvoid* thisAddr = cast(void*) &this; Error compile: Deprecation: this is not an lvalue
Mar 13 2016
On Sunday, 13 March 2016 at 16:16:55 UTC, MGW wrote:void* thisAddr = cast(void*) &this;This doesn't really make sense anyway, this is a local variable, you want to do cast(void*) this in a class if you need the address (which btw, you shouldn't actually, the reference itself ought to be enough)
Mar 13 2016