Passing this to void *
Nicholas Wilson
iamthewilsonator at hotmail.com
Thu Nov 23 05:57:01 UTC 2017
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);
> }
Note that all the examples and advice in this thread apply to
_classes_, not to structs.
The 'this' in a class is a pointer, since classes are reference
types (like the 'this' pointer in C++) so you can cast it to a
void* directly.
The 'this' in a struct is a value, since structs are value types.
So if you want the address of the struct from within one of the
struct's methods you need to use the & operator (then it becomes
a pointer and you can cast it to a void* .
More information about the Digitalmars-d-learn
mailing list