Using structs to mimic the C++ object structure
With D's low-level control, we can access any kind of data structure and paint nice types over it. To demonstrate this, we'll access a C++ object from D. The same technique can also be used for memory-mapped hardware.
Note
You need to take caution that this is not portable. It may not work even across different versions of the same C++ compiler. If you use this trick, make sure it is in an environment where you know the binary layout.
How to do it…
Let's mimic the C++ object structure by executing the following steps:
Investigate the C++ ABI on your system. Typically, but not necessarily, a C++ object consists of a pointer to the virtual function table, the members of the parent class (recursively), and the members of the child class. If the C++ object has no virtual functions, it does not have a virtual table; the layout is then compatible with a C struct with the same data members.
Create an
extern(C++)
interface for the virtual table, like we...