| Thread overview | |||||
|---|---|---|---|---|---|
|
June 25, 2017 Interfacing with C - calling member function of D struct from C? | ||||
|---|---|---|---|---|
| ||||
Hello! If I have a D struct like:
struct Foo
{
int bar;
void addToBar(int what) {
bar += what;
}
}
How would I call `addToBar` from C code? Would I need to put the `addToBar` function outside of the struct and mark it as `extern (C)` and in normal D code take advantage of UFCS or is there some magic C incantation?
I've scoured the forums and other places for anything about this but couldn't find any information whatsoever... so yeah. (or my Google-fu is terrible)
Thanks!
| ||||
June 25, 2017 Re: Interfacing with C - calling member function of D struct from C? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to unleashy | On Sunday, 25 June 2017 at 02:05:35 UTC, unleashy wrote:
> How would I call `addToBar` from C code?
You don't. Instead write it like:
struct Foo
{
int bar;
}
extern(C) void addToBar(Foo* foo, int what) {
foo.bar += what;
}
Then define it in C the same way and you call it the normal way from C.
But from D, you can UFCS call it:
Foo* foo = new Foo();
foo.addToBar(5); // cool
though I'd prolly just call it from D the same way you do from C too.
| |||
June 25, 2017 Re: Interfacing with C - calling member function of D struct from C? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Sunday, 25 June 2017 at 02:09:53 UTC, Adam D. Ruppe wrote:
> On Sunday, 25 June 2017 at 02:05:35 UTC, unleashy wrote:
>> How would I call `addToBar` from C code?
>
> You don't. Instead write it like:
>
> struct Foo
> {
> int bar;
> }
>
> extern(C) void addToBar(Foo* foo, int what) {
> foo.bar += what;
> }
>
>
> Then define it in C the same way and you call it the normal way from C.
>
> But from D, you can UFCS call it:
>
> Foo* foo = new Foo();
> foo.addToBar(5); // cool
>
>
> though I'd prolly just call it from D the same way you do from C too.
Thank you, this is what I suspected :)
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply