Reference to D class instance with a C library

Leandro Motta Barros lmb at stackedboxes.org
Sat Jul 13 11:13:03 PDT 2013


Hello,

TL;DR: Can I somehow store a reference to a D class instance in a
field of a struct from a C library I am using with my D code?

The long story:

I am writing some D code that uses a C library. This C library
provides an event handling mechanism in which events are represented
by (plain C) structs that look like this:

struct event
{
   // ...lots of event stuff...
   intptr_t user_data;
}

I would like to create an event struct that contains a reference to a
D class instance in that 'user_data' field.

As I understand (and from bad experiences in the past), object
references in D are not exactly the same thing as pointers, so I
cannot simply take the address of the class instance and stash it in
that 'user_data' field.

So, is there some way to store a reference to a D class instance in
that 'user_data' field? (I can guarantee that the D object will be
alive whenever that 'event' struct is used.)

Thanks!

LMB


More information about the Digitalmars-d-learn mailing list