Interfacing to const T& or const T* C++ code

Atila Neves via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jun 1 01:51:27 PDT 2014


So I was mucking about with calling C++ from D yesterday and was
pleasantly surprised that this worked:

D:
      struct Foo { int i; int j; }
      extern(C++) void useFoo(ref const(Foo) foo);

C++:
      struct Foo { int i; int j; };
      void useFoo(const Foo& foo) { ... }

In fact, omitting const on either side causes a link error, and
the same for using a pointer instead of a reference on one of the
sides. All good. But this doesn't work:

D:
      extern(C++) interface DClass { int getIndex() const; }
      extern(C++) void useObj(in DClass dclass); //I also tried
const(DClass)

C++:

      struct DClass { virtual int getIndex() const = 0; }
      void useObj(const DClass* dclass); //DClass* works though!

I can make `DClass` `const` or `in` in D as much as I want, but
it fails to link if I add const on the C++ side. Is there a
reason why it works for structs (as it should) but not classes?
It'd be nice to be able to pass classes in by const T&. Or even
const T*!

Atila


More information about the Digitalmars-d-learn mailing list