Thread overview | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
November 22, 2017 Passing this to void * | ||||
---|---|---|---|---|
| ||||
I am a C++ game developer and I want to give it a try. It seems "this" in Dlang is a reference instead of pointer. How can I pass it as void *? void foo(void *); class Pizza { public: this() { Pizza newone = this; // works but newone is actually not this pizza. foo(&newone); // this does not work.. foo(this); } } void main() { Pizza pizza = new Pizza(); // this works... foo(&pizza); } |
November 22, 2017 Re: Passing this to void * | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tim Hsu | On Wednesday, 22 November 2017 at 15:07:08 UTC, Tim Hsu wrote:
> I am a C++ game developer and I want to give it a try.
>
> It seems "this" in Dlang is a reference instead of pointer.
>
> How can I pass it as void *?
>
> void foo(void *);
>
> class Pizza {
> public:
> this() {
> Pizza newone = this;
> // works but newone is actually not this pizza.
> foo(&newone);
> // this does not work..
> foo(this);
> }
> }
>
> void main() {
> Pizza pizza = new Pizza();
> // this works...
> foo(&pizza);
> }
&this will do.
|
November 22, 2017 Re: Passing this to void * | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stefan Koch | On Wednesday, 22 November 2017 at 15:07:54 UTC, Stefan Koch wrote:
> On Wednesday, 22 November 2017 at 15:07:08 UTC, Tim Hsu wrote:
>> I am a C++ game developer and I want to give it a try.
>>
>> It seems "this" in Dlang is a reference instead of pointer.
>>
>> How can I pass it as void *?
>>
>> void foo(void *);
>>
>> class Pizza {
>> public:
>> this() {
>> Pizza newone = this;
>> // works but newone is actually not this pizza.
>> foo(&newone);
>> // this does not work..
>> foo(this);
>> }
>> }
>>
>> void main() {
>> Pizza pizza = new Pizza();
>> // this works...
>> foo(&pizza);
>> }
>
> &this will do.
I've tried it in the first place.
...
Error: this is not an lvalue
|
November 22, 2017 Re: Passing this to void * | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tim Hsu | On Wednesday, 22 November 2017 at 15:11:08 UTC, Tim Hsu wrote:
> On Wednesday, 22 November 2017 at 15:07:54 UTC, Stefan Koch
>>
>> &this will do.
>
> I've tried it in the first place.
>
> ...
>
> Error: this is not an lvalue
In that case casting to void* should be fine.
|
November 22, 2017 Re: Passing this to void * | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stefan Koch | On Wednesday, 22 November 2017 at 15:14:32 UTC, Stefan Koch wrote:
> On Wednesday, 22 November 2017 at 15:11:08 UTC, Tim Hsu wrote:
>> On Wednesday, 22 November 2017 at 15:07:54 UTC, Stefan Koch
>>>
>>> &this will do.
>>
>> I've tried it in the first place.
>>
>> ...
>>
>> Error: this is not an lvalue
>
> In that case casting to void* should be fine.
But..The compiler still does not produce the executable...
foo(&this);
app.d(17): Error: this is not an lvalue
|
November 22, 2017 Re: Passing this to void * | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stefan Koch | On Wednesday, 22 November 2017 at 15:07:54 UTC, Stefan Koch wrote:
> &this will do.
Even if it were an lvalue, that would be the address of a local. You should basically NEVER do that with D classes.
Just `cast(void*) this` if you must pass it to such a function.
|
November 22, 2017 Re: Passing this to void * | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Wednesday, 22 November 2017 at 15:17:33 UTC, Adam D. Ruppe wrote: > On Wednesday, 22 November 2017 at 15:07:54 UTC, Stefan Koch wrote: >> &this will do. > > Even if it were an lvalue, that would be the address of a local. You should basically NEVER do that with D classes. > > Just `cast(void*) this` if you must pass it to such a function. I am afraid what will happen when casting this reference to void * glfwSetWindowUserPointer gives us a chance to provide a pointer to userdata. so that in callback function, we can retrieve the data and don't have to declare global variable. class App { public this() { m_window = glfwCreateWindow(); glfwSetWindowUserPointer(m_window, cast(void *)(&this)); } } How do I use this function in Dlang? sorry for my bad english. |
November 22, 2017 Re: Passing this to void * | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Wednesday, 22 November 2017 at 15:17:33 UTC, Adam D. Ruppe wrote:
> On Wednesday, 22 November 2017 at 15:07:54 UTC, Stefan Koch wrote:
>> &this will do.
>
> Even if it were an lvalue, that would be the address of a local. You should basically NEVER do that with D classes.
>
> Just `cast(void*) this` if you must pass it to such a function.
It's worth noting that you will still be passing different addresses to foo(void*) because classes are reference types in D (structs are not). In the constructor you're passing the address of the class object itself, but in the main function you're passing the address of the reference.
|
November 22, 2017 Re: Passing this to void * | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tim Hsu | On Wednesday, 22 November 2017 at 15:23:58 UTC, Tim Hsu wrote:
>
> I am afraid what will happen when casting this reference to void *
>
a ref is a ptr.
The cast will produce a ptr which is valid as long as the ref is valid.
|
November 22, 2017 Re: Passing this to void * | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dukc | On Wednesday, 22 November 2017 at 15:27:27 UTC, Dukc wrote:
> On Wednesday, 22 November 2017 at 15:17:33 UTC, Adam D. Ruppe wrote:
>> On Wednesday, 22 November 2017 at 15:07:54 UTC, Stefan Koch wrote:
>>> &this will do.
>>
>> Even if it were an lvalue, that would be the address of a local. You should basically NEVER do that with D classes.
>>
>> Just `cast(void*) this` if you must pass it to such a function.
>
> It's worth noting that you will still be passing different addresses to foo(void*) because classes are reference types in D (structs are not). In the constructor you're passing the address of the class object itself, but in the main function you're passing the address of the reference.
It seems in D, reference has its own address, am I right? unlike c++
|
Copyright © 1999-2021 by the D Language Foundation