D & C++ class question

Nicholas Wilson iamthewilsonator at hotmail.com
Wed Nov 28 10:44:05 UTC 2018


On Wednesday, 28 November 2018 at 07:22:46 UTC, bauss wrote:
> If I have a class from D.
>
> How would you use that class in C++?
>
> Like what's the correct approach to this.
>
> Would it work just by doing "extern(C++)" or will that only 
> work for D to use C++ classes?

If you have

foo.d
----
class Foo
{
     int aMember;
     void aMethod() { ... };
     final aFinalMethod() { ... };
}
----
to expose that to C++ you need to add extern(C++) too Foo and use 
it in C++ as a Foo*
foo.h
----
class Foo
{
     int aMember;
     virtual void aMethod();
     void aFinalMethod();
};
----
If you can't/don't want to do that you can always pass it around 
in C++ as an opaque pointer with functions that call the member 
functions and get/set the variables (i.e. the pimpl strategy).


More information about the Digitalmars-d-learn mailing list