Is D so powerfull ??
Daniel Murphy via Digitalmars-d
digitalmars-d at puremagic.com
Sun Nov 8 23:00:46 PST 2015
On 9/11/2015 5:54 PM, Jeremy DeHaan wrote:
>>
>> Didn't you say constructors and destructors are missing? What should
>> one do in those cases?
Constructors and destructors do not match the C++ ABI, but they are
still generated, so they can only be called from the language they were
written in. So if you write your classes in D you must new and delete
(or GC) them from D. You can still create them from C++ (or create
C++-written classes from D) if you use a forwarding function:
X makeX() { return new X(); }
>
> Additionally, should we use new in this case? Wouldn't new create a
> pointer to a C++ class? Or does it work differently for extern(c++)
> classes?
New in D will allocate a class instance on the GC heap and return a
reference to it, just like when it's used with D classes.
These two functions have the same ABI:
// D
extern(C++) class X {}
extern(C++) void func(X x);
// C++
class X {}
void func(X *x);
You can find several examples of C++ interop in dmd's test\runnable\cppa.d
More information about the Digitalmars-d
mailing list