I have gone through the docs explaining how to call C++ from D explained here: http://dlang.org/cpp_interface.html . There are a however a couple of things that are not quite clear to me.
Taking the example provided on the D website:
#include <iostream>
using namespace std;
class D {
public:
virtual int bar(int i, int j, int k)
{
cout << "i = " << i << endl;
cout << "j = " << j << endl;
cout << "k = " << k << endl;
return 8;
}
};
D *getD() {
D *d = new D();
return d;
}
The C++ class can then be called from D as shown below:
extern (C++) {
interface D {
int bar(int i, int j, int k);
}
D getD();
}
void main() {
D d = getD();
d.bar(9,10,11);
}
What is not quite clear to me is how the C++ object gets deleted. Does the D garbage collector call delete on the C++ object or do we need to provide a "deleter" function which deletes the object and call it manually from D? It seems to me that if I add a destructor to the C++ class, it is never called. Also I noticed that the C++ class must declare the member functions in the exact same order as they are declared in the D interface (for example if I add a destructor before the bar() method, the C++ object cannot be called from D, but if the destructor is declared after the bar() method, everything works fine).
Also if the D interface is defined as:
extern(C++){
interface D{
int bar();
int foo();
}
}
And the correspondind C++ class is given by:
class D{
public:
virtual int bar(){};
virtual int foo(){};
};
How can you guarantee that the C++ virtual methods vtbl will be created in the same order as the methods declared in the D interface. To me there is no guarantee for this. In other words, how can we be sure that D::bar() will be in the first position in the vtbl? Is this not implementation/compiler dependant?