Thread overview
Get address of object in constructor.
Mar 13, 2016
MGW
Mar 13, 2016
WebFreak001
Mar 13, 2016
MGW
Mar 13, 2016
WebFreak001
Mar 13, 2016
MGW
Mar 13, 2016
Adam D. Ruppe
March 13, 2016
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();

March 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)
March 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:
>> 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)

Super! Ok!
March 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
March 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; instead

void* thisAddr = cast(void*) &this;

Error compile: Deprecation: this is not an lvalue


March 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)