Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
November 28, 2018 D & C++ class question | ||||
---|---|---|---|---|
| ||||
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? |
November 28, 2018 Re: D & C++ class question | ||||
---|---|---|---|---|
| ||||
Posted in reply to bauss | 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).
|
November 28, 2018 Re: D & C++ class question | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nicholas Wilson | On Wednesday, 28 November 2018 at 10:44:05 UTC, Nicholas Wilson wrote:
> 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).
Well unfortunately I cannot control the C++ side of things, but I assume that it'll work properly with extern(C++) so I guess I will just go ahead and try and see if everything works out. I have access to the C++ source so at the very least I can see how it's used there and if it's possible in the way I want it to. I don't hope I have to have a separate C++ "bridge" to make it work.
|
November 28, 2018 Re: D & C++ class question | ||||
---|---|---|---|---|
| ||||
Posted in reply to bauss | On Wednesday, 28 November 2018 at 13:13:40 UTC, bauss wrote:
> Well unfortunately I cannot control the C++ side of things, but I assume that it'll work properly with extern(C++) so I guess I will just go ahead and try and see if everything works out. I have access to the C++ source so at the very least I can see how it's used there and if it's possible in the way I want it to. I don't hope I have to have a separate C++ "bridge" to make it work.
Its mostly a magling problem, if the C++ API uses class& that you have to interface with then you have a problem that would require a shim.
|
November 28, 2018 Re: D & C++ class question | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nicholas Wilson | On Wednesday, 28 November 2018 at 13:42:43 UTC, Nicholas Wilson wrote:
> On Wednesday, 28 November 2018 at 13:13:40 UTC, bauss wrote:
>> Well unfortunately I cannot control the C++ side of things, but I assume that it'll work properly with extern(C++) so I guess I will just go ahead and try and see if everything works out. I have access to the C++ source so at the very least I can see how it's used there and if it's possible in the way I want it to. I don't hope I have to have a separate C++ "bridge" to make it work.
>
> Its mostly a magling problem, if the C++ API uses class& that you have to interface with then you have a problem that would require a shim.
It looks like every class is passed by pointers, so that's a relief on that part I guess.
|
November 29, 2018 Re: D & C++ class question | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nicholas Wilson | On Wednesday, 28 November 2018 at 13:42:43 UTC, Nicholas Wilson wrote:
> On Wednesday, 28 November 2018 at 13:13:40 UTC, bauss wrote:
>> Well unfortunately I cannot control the C++ side of things, but I assume that it'll work properly with extern(C++) so I guess I will just go ahead and try and see if everything works out. I have access to the C++ source so at the very least I can see how it's used there and if it's possible in the way I want it to. I don't hope I have to have a separate C++ "bridge" to make it work.
>
> Its mostly a magling problem, if the C++ API uses class& that you have to interface with then you have a problem that would require a shim.
Are there no support for references with mangling in D?
Like what about int&?
|
November 29, 2018 Re: D & C++ class question | ||||
---|---|---|---|---|
| ||||
Posted in reply to bauss | On Thursday, 29 November 2018 at 16:24:58 UTC, bauss wrote:
> Are there no support for references with mangling in D?
>
> Like what about int&?
Of course, that's `ref int`. But a `ref CppClass` is C++-mangled as `CppClass* &`.
|
November 29, 2018 Re: D & C++ class question | ||||
---|---|---|---|---|
| ||||
Posted in reply to kinke | On Thursday, 29 November 2018 at 17:12:28 UTC, kinke wrote:
> On Thursday, 29 November 2018 at 16:24:58 UTC, bauss wrote:
>> Are there no support for references with mangling in D?
>>
>> Like what about int&?
>
> Of course, that's `ref int`. But a `ref CppClass` is C++-mangled as `CppClass* &`.
I was hoping for that and glad to know that was the only issue.
|
Copyright © 1999-2021 by the D Language Foundation