Jump to page: 1 24  
Page
Thread overview
How to get the pointer of "this" ?
May 24, 2020
Vinod K Chandran
May 24, 2020
bauss
May 25, 2020
John Burton
May 25, 2020
Mike Parker
May 25, 2020
John Burton
May 26, 2020
Vinod K Chandran
May 26, 2020
Johannes Loher
May 26, 2020
Johannes Loher
May 26, 2020
Vinod K Chandran
May 26, 2020
Vinod K Chandran
May 26, 2020
bauss
May 27, 2020
Johannes Loher
May 25, 2020
Vinod K Chandran
May 25, 2020
Mike Parker
May 25, 2020
Vinod K Chandran
May 25, 2020
bauss
May 25, 2020
Vinod K Chandran
May 26, 2020
Vinod K Chandran
May 26, 2020
John Chapman
May 26, 2020
Vinod K Chandran
May 26, 2020
Vinod K Chandran
May 26, 2020
ag0aep6g
May 26, 2020
Vinod K Chandran
May 26, 2020
Johannes Loher
May 26, 2020
John Chapman
May 25, 2020
welkam
May 25, 2020
Adam D. Ruppe
May 25, 2020
Vinod K Chandran
May 25, 2020
Adam D. Ruppe
May 25, 2020
Vinod K Chandran
May 25, 2020
Adam D. Ruppe
May 26, 2020
Vinod K Chandran
May 26, 2020
Adam D. Ruppe
May 26, 2020
Vinod K Chandran
May 24, 2020
Hi all,
I have a class like this.
class Button : Control {
	...
	HWND createButton(){
		...
		SetWindowSubclass(this.mHandle, SUBCLASSPROC(&btnWndProc), UINT_PTR(subClsID), cast(DWORD_PTR) this);
	}
}

But compiler says that - "Error: 'this' is not an lvalue and cannot be modified"
I've seen many cpp code which uses pointer to 'this' as the last parameter in SetWindowSubclass(). Is there something wrong with my approach ?
May 24, 2020
On Sunday, 24 May 2020 at 17:05:16 UTC, Vinod K Chandran wrote:
> Hi all,
> I have a class like this.
> class Button : Control {
> 	...
> 	HWND createButton(){
> 		...
> 		SetWindowSubclass(this.mHandle, SUBCLASSPROC(&btnWndProc), UINT_PTR(subClsID), cast(DWORD_PTR) this);
> 	}
> }
>
> But compiler says that - "Error: 'this' is not an lvalue and cannot be modified"
> I've seen many cpp code which uses pointer to 'this' as the last parameter in SetWindowSubclass(). Is there something wrong with my approach ?

I think your issue might be elsewhere because casting this should be fine and it should not complain about that in your given code.

At least you should be able to pass this to another function or even cast it.

Please show the full code and the full error which gives you the stacktrace of where it's called and from where.
May 25, 2020
On Sunday, 24 May 2020 at 17:40:10 UTC, bauss wrote:
> On Sunday, 24 May 2020 at 17:05:16 UTC, Vinod K Chandran wrote:
>> [...]
>
> I think your issue might be elsewhere because casting this should be fine and it should not complain about that in your given code.
>
> At least you should be able to pass this to another function or even cast it.
>
> Please show the full code and the full error which gives you the stacktrace of where it's called and from where.


I believe that in D *this* is a reference to the
object and not a pointer like in C++.
So I think that writing &this might be what you need?
May 25, 2020
On Sunday, 24 May 2020 at 17:40:10 UTC, bauss wrote:
> On Sunday, 24 May 2020 at 17:05:16 UTC, Vinod K Chandran wrote:
>> [...]
>
> I think your issue might be elsewhere because casting this should be fine and it should not complain about that in your given code.
>
> At least you should be able to pass this to another function or even cast it.
>
> Please show the full code and the full error which gives you the stacktrace of where it's called and from where.

Here is my full code. Please take a look.
https://pastebin.com/av3nrvtT
May 25, 2020
On Monday, 25 May 2020 at 08:39:23 UTC, John Burton wrote:

> I believe that in D *this* is a reference to the
> object and not a pointer like in C++.
> So I think that writing &this might be what you need?

No. A class reference is a pointer under the hood. Getting its address will result in a pointer to the reference variable itself, not to the class instance. When passing a reference to a C API, casting it directly to the C type is correct.
May 25, 2020
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

The error has nothing to do with taking a pointer to `this`. It's suggesting that somewhere in your code you're attempting to use the `this` reference like an lvalue, e.g. making an assignment to it. I don't see anywhere that you're doing that. Glancing through the code, I don't see anywhere that you're doing that (and unfortunately this is not a minimal example because of dependencies on some of your other modules, so I can't compile it myself).

What was the line number of the original error? Sometimes the line number reported isn't where the error actually occurs. Also, try to see if you can minimize it so that someone else can compile it.

A couple of points about your code unrelated to your error:

* `private static int btnNumber = 1;` -- `static` has no meaning at module scope. You'll see it in C code, but in D `private` provides the same functionality. You can delete `static` from those module scope declarations.

* `with(this){` -- you absolutely do not need to do this. `this` is never required as a prefix on any member unless, e.g., a member variable and another more locally scoped variable have the same name, like `this.x = x` in a setter function to distinguish the member variable from the function parameter. You're already using the convention of naming your member variables with a `m` prefix, so you won't run into that issue. `this.mFoo` is exactly the same as `mFoo`, and with(this) is pointless. I mean, if you want to use `this.mFoo` as a stylistic choice, that's your call (though the `m` makes it redundant really), but even then using `with(this)` serves no purpose.





May 25, 2020
On Monday, 25 May 2020 at 16:54:11 UTC, Mike Parker wrote:
> On Monday, 25 May 2020 at 16:26:31 UTC, Vinod K Chandran wrote:
>
>> [...]
>
> The error has nothing to do with taking a pointer to `this`. It's suggesting that somewhere in your code you're attempting to use the `this` reference like an lvalue, e.g. making an assignment to it. I don't see anywhere that you're doing that. Glancing through the code, I don't see anywhere that you're doing that (and unfortunately this is not a minimal example because of dependencies on some of your other modules, so I can't compile it myself).
>
> [...]

Hi @Mike Parker,
Thank you for your valuable suggestions. I will sure follow them. Well, the  exact line number where the error showing is the one with the "SetWindowSubclass" function.  In pastebin, the line number is 124.
May 25, 2020
On Monday, 25 May 2020 at 16:39:30 UTC, Mike Parker wrote:
> On Monday, 25 May 2020 at 08:39:23 UTC, John Burton wrote:
>
>> I believe that in D *this* is a reference to the
>> object and not a pointer like in C++.
>> So I think that writing &this might be what you need?
>
> No. A class reference is a pointer under the hood. Getting its address will result in a pointer to the reference variable itself, not to the class instance. When passing a reference to a C API, casting it directly to the C type is correct.

Ah I see.
In that case I have some code I need to investigate as it's probably only working by accident!
May 25, 2020
On Monday, 25 May 2020 at 17:14:13 UTC, Vinod K Chandran wrote:
> On Monday, 25 May 2020 at 16:54:11 UTC, Mike Parker wrote:
>> On Monday, 25 May 2020 at 16:26:31 UTC, Vinod K Chandran wrote:
>>
>>> [...]
>>
>> The error has nothing to do with taking a pointer to `this`. It's suggesting that somewhere in your code you're attempting to use the `this` reference like an lvalue, e.g. making an assignment to it. I don't see anywhere that you're doing that. Glancing through the code, I don't see anywhere that you're doing that (and unfortunately this is not a minimal example because of dependencies on some of your other modules, so I can't compile it myself).
>>
>> [...]
>
> Hi @Mike Parker,
> Thank you for your valuable suggestions. I will sure follow them. Well, the  exact line number where the error showing is the one with the "SetWindowSubclass" function.  In pastebin, the line number is 124.

Need to see the Control  class too.

I think the problem might be something you're referencing from there but need to be sure.
May 25, 2020
On Monday, 25 May 2020 at 18:42:33 UTC, bauss wrote:
> On Monday, 25 May 2020 at 17:14:13 UTC, Vinod K Chandran wrote:
>> On Monday, 25 May 2020 at 16:54:11 UTC, Mike Parker wrote:
>>> [...]
>>
>> Hi @Mike Parker,
>> Thank you for your valuable suggestions. I will sure follow them. Well, the  exact line number where the error showing is the one with the "SetWindowSubclass" function.  In pastebin, the line number is 124.
>
> Need to see the Control  class too.
>
> I think the problem might be something you're referencing from there but need to be sure.

@bauss,
Here is the code for Control class.
https://pastebin.com/Hy9dCNdS
« First   ‹ Prev
1 2 3 4