How to get the pointer of "this" ?

Johannes Loher johannes.loher at fg4f.de
Tue May 26 14:22:45 UTC 2020


Am 26.05.20 um 16:17 schrieb Vinod K Chandran:
> On Tuesday, 26 May 2020 at 13:48:52 UTC, ag0aep6g wrote:
>> On 26.05.20 15:43, Vinod K Chandran wrote:
>>> So far now, two solutions are very clear for this problem.
>>> 1. As per John Chapman's suggestion - use
>>> cast(DWORD_PTR)cast(void*)this).
>>> 2. Use another varibale to use as an lvalue. -
>>>       Button dummyBtn = this;
>>>       cast(DWORD_PTR)&dummyBtn;
>>> Among these two, i think 2nd option is good. Am i right ?
>>
>> No. Use option 1.
> 
> Could you please tell me why ?
With option 2, you will not actually get a pointer to the object but a
pointer to the local variable which is a reference to the object. `this`
and the local variable both are references to the object, so internally
they already are pointers. Their usage is just restricted in regular D
code. This is also why simply casting it to a pointer works. You need to
cast to void* in between because that's just the way to tell the
compiler to ignore the restrictions you usually have for references.


More information about the Digitalmars-d-learn mailing list