Fun with extern(C++)

Jacob Carlborg via Digitalmars-d digitalmars-d at puremagic.com
Tue Jan 26 05:58:59 PST 2016


On 2016-01-26 13:59, Manu via Digitalmars-d wrote:

> Hmmm. I wonder if this will cause problems...
> Is it impossible to support a C++ class implementing a D interface?

No that I know how the C++ compatibility works but I would guess the 
compiler needs to know at compile time how to call a method. If it would 
be possible to implement a D interface as either a D class or C++ class. 
The compiler cannot know which runtime type a variable which is declared 
as an interface can hold.

interface A { void foo(); }
class B : A { void foo(); }
extern(C++) class C : A { void foo(); } // assuming this works

A a = new B;
A b = new C; // assuming this works

a.foo();
b.foo();

When the compiler sees "a" or "b", how should it know it should call 
"foo" as a D method or C++ method? It only knows about the static type 
which is A, a D interface.

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list