May 26, 2020 Re: How to get the pointer of "this" ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Johannes Loher | On Tuesday, 26 May 2020 at 12:08:29 UTC, Johannes Loher wrote:
> [...]
Small correction: I said that this is an lvalue and that you cannot take the address of lvalues. Of course that is incorrect, I meant to say that rvalues (this is an rvalue and you cannot take the address of rvalues).
|
May 26, 2020 Re: How to get the pointer of "this" ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vinod K Chandran | On Monday, 25 May 2020 at 16:26:31 UTC, Vinod K Chandran wrote: > Here is my full code. Please take a look. > https://pastebin.com/av3nrvtT Change line 124 to: SetWindowSubclass(this.mHandle, SUBCLASSPROC(&btnWndProc), UINT_PTR(subClsID), cast(DWORD_PTR)cast(void*)this); That is, change `&this` to `cast(void*)this`. |
May 26, 2020 Re: How to get the pointer of "this" ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Chapman | On Tuesday, 26 May 2020 at 12:41:20 UTC, John Chapman wrote: > On Monday, 25 May 2020 at 16:26:31 UTC, Vinod K Chandran wrote: >> Here is my full code. Please take a look. >> https://pastebin.com/av3nrvtT > > Change line 124 to: > > SetWindowSubclass(this.mHandle, SUBCLASSPROC(&btnWndProc), UINT_PTR(subClsID), cast(DWORD_PTR)cast(void*)this); > > That is, change `&this` to `cast(void*)this`. Hi, Thanks for the reply. That will work like charm but we need to change the code in subclassed button's WndProc like this-- extern(Windows) private LRESULT btnWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam, UINT_PTR scID, DWORD_PTR refData) { try { Button thisBtn = cast(Button)cast(void*)refData; { catch (Exception e) {} .... |
May 26, 2020 Re: How to get the pointer of "this" ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Johannes Loher | On Tuesday, 26 May 2020 at 12:08:29 UTC, Johannes Loher wrote:
> On Tuesday, 26 May 2020 at 11:44:58 UTC, Vinod K Chandran wrote:
>> [...]
>
>> [...]
> It doesn't compile, the line
>
> string mt
>
> [...]
Hi,
Sorry for the typos in my code.
|
May 26, 2020 Re: How to get the pointer of "this" ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vinod K Chandran | On Tuesday, 26 May 2020 at 13:37:22 UTC, Vinod K Chandran wrote: > On Tuesday, 26 May 2020 at 12:41:20 UTC, John Chapman wrote: >> On Monday, 25 May 2020 at 16:26:31 UTC, Vinod K Chandran wrote: >>> Here is my full code. Please take a look. >>> https://pastebin.com/av3nrvtT >> >> Change line 124 to: >> >> SetWindowSubclass(this.mHandle, SUBCLASSPROC(&btnWndProc), UINT_PTR(subClsID), cast(DWORD_PTR)cast(void*)this); >> >> That is, change `&this` to `cast(void*)this`. > > Hi, > Thanks for the reply. That will work like charm but we need to change the code in subclassed button's WndProc like this-- > extern(Windows) > private LRESULT btnWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam, UINT_PTR scID, DWORD_PTR refData) { > try { > > Button thisBtn = cast(Button)cast(void*)refData; > { > catch (Exception e) {} > .... 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 ? |
May 26, 2020 Re: How to get the pointer of "this" ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vinod K Chandran | On Tuesday, 26 May 2020 at 11:35:23 UTC, Vinod K Chandran wrote:
> Okay, but uint is working perfectly.
It won't if you use -m64.
|
May 26, 2020 Re: How to get the pointer of "this" ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vinod K Chandran | 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.
|
May 26, 2020 Re: How to get the pointer of "this" ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ag0aep6g | 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 ?
|
May 26, 2020 Re: How to get the pointer of "this" ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Tuesday, 26 May 2020 at 13:44:24 UTC, Adam D. Ruppe wrote:
> On Tuesday, 26 May 2020 at 11:35:23 UTC, Vinod K Chandran wrote:
>> Okay, but uint is working perfectly.
>
> It won't if you use -m64.
Okay. I got it.
|
May 26, 2020 Re: How to get the pointer of "this" ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vinod K Chandran | 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.
|
Copyright © 1999-2021 by the D Language Foundation