Using a delegate when interfacing with C

Adam D. Ruppe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jul 5 15:28:47 PDT 2014


On Saturday, 5 July 2014 at 22:18:56 UTC, Marco Cosentino wrote:
>        auto client = *(cast(ClientImplementation*) data);

Try just

auto client = cast(ClientImplementation) data;


and

      this.setProcessCallback(callback, cast(void *) &this);

setProcessCallback(callback, cast(void*) this);

> Can somebody help me in figuring out why this happens?


The reason is a class this in D is already a pointer (just a 
hidden one) so when you do &this in a class, it is like a 
ClientImplementation** in C - a pointer to a (temporary) pointer. 
So by the time the callback runs, it is pointing to nonsense.

In general, remember any class reference in D is already 
equivalent to a pointer in C or C++ and can be casted straight to 
void* without needing to take its address.


More information about the Digitalmars-d-learn mailing list