C++;到D互操作性 从我开始尝试调用C++的一些D代码(用C++和D定义的类/接口)。

C++;到D互操作性 从我开始尝试调用C++的一些D代码(用C++和D定义的类/接口)。,c++,d,C++,D,D代码 module BufferCppBinding; extern (C++) void *createBufferCppBinding() { BufferCppBinding ptr = new BufferCppBinding(); return cast(void*)ptr; } extern (C++) interface BufferCppBindingInterface { void construct(); // ... } class

D代码

module BufferCppBinding;

extern (C++) void *createBufferCppBinding() {
    BufferCppBinding ptr = new BufferCppBinding();
    return cast(void*)ptr;
}

extern (C++) interface BufferCppBindingInterface {
    void construct();
    // ...
}

class BufferCppBinding : BufferCppBindingInterface {
    public Buffer thisPtr;

    public extern (C++) void construct() {
        // doesn't do anything
    }
}

将C++声明为C++的代码:

class BufferCppBinding {
public:

    virtual void construct();
};
对于D运行时的初始化,我在D中编写了一个小函数,该函数在D中运行:

extern (C++) void initDRuntime() nothrow{
    try
    {
        Runtime.initialize();
        //result = myWinMain(hInstance, hPrevInstance, lpCmdLine, iCmdShow);
        //Runtime.terminate(&exceptionHandler);
    }
    catch (Throwable o)
    {
        //MessageBox(null, o.toString().toUTF16z, "Error", MB_OK | MB_ICONEXCLAMATION);
        //result = 0;
    }
}
用法(C++):

BufferCppBinding*vertexBuffer=reinterpret_cast(createBufferCppBinding());
//这里发生了车祸
vertexBuffer->construct();
我正在使用g++5.2和ldc2编译代码,并将其与ldc2链接


<>我只得到一个SigSeV.< /P> < P>指向C++堆的指针指向GC堆是个坏主意——使用C++ > Malc < /Calp>/<代码> ESPOTE < /C> >(或<代码> STD.Time.Apalast.make),并在C++端调用< /COD>自由<代码>。不过,这不会运行析构函数,因此可能需要公开一个调用destroy`的D函数


btw,不需要返回<代码>空白>代码>并返回-只返回<代码> BufferCppBindingInterface <代码> > <代码>缓冲区> />代码> < /p>您的C++声明是否应该是D接口(Buffer-CppbDIdIn接口)的声明?
BufferCppBinding *vertexBuffer = reinterpret_cast<BufferCppBinding*>(createBufferCppBinding());

// here happens the crash
vertexBuffer->construct();