Re: Interfacing with basic C++ class

2022-10-02 Thread Riccardo M via Digitalmars-d-learn
On Friday, 30 September 2022 at 22:56:06 UTC, Ogi wrote: On Thursday, 29 September 2022 at 12:49:06 UTC, Riccardo M wrote: When interfacing to C++, disregard the keyword and look at the implementation instead. If all its member functions are non-virtual, map it to struct. Otherwise map it to cl

Re: Interfacing with basic C++ class

2022-09-30 Thread Ogi via Digitalmars-d-learn
On Thursday, 29 September 2022 at 12:49:06 UTC, Riccardo M wrote: On Thursday, 29 September 2022 at 11:13:15 UTC, Ogi wrote: So it turns out that D's structs are a much better match for C++'s classes in this case. But why is this? Can you elaborate? It must have to do with the fact that D struc

Re: Interfacing with basic C++ class

2022-09-29 Thread Riccardo M via Digitalmars-d-learn
On Thursday, 29 September 2022 at 11:13:15 UTC, Ogi wrote: Ali is correct. However, you don’t have to change anything on the C++ side, just map C++ class to D struct: [...] Thanks, this works perfectly. Now i see that I can even instantiate directly from D, calling the default ctor, calling t

Re: Interfacing with basic C++ class

2022-09-29 Thread Ali Çehreli via Digitalmars-d-learn
On 9/29/22 01:28, Riccardo M wrote: > if one should > slightly rearrange C++ code as well. Right. Additionally, the order of members must match (I am pretty sure, which means I am not :p). > I am a recent addition to D > language :) Welcome! :) > Do you know that this is documented somewher

Re: Interfacing with basic C++ class

2022-09-29 Thread Ogi via Digitalmars-d-learn
On Wednesday, 28 September 2022 at 19:57:10 UTC, Riccardo M wrote: I think I am stuck in the easiest of the issues and yet it seems I cannot get around this. I have a C++ file: ``` class MyClass { public: int field; MyClass(int a) : field(a) {} int add(int asd) { return asd

Re: Interfacing with basic C++ class

2022-09-29 Thread Riccardo M via Digitalmars-d-learn
On Wednesday, 28 September 2022 at 20:41:13 UTC, Ali Çehreli wrote: [...] Ali Thank you, that is perfect! However that begs the following observation: it would be rather hard to link to a C++ library only by means of 'extern (C++)' if one should slightly rearrange C++ code as well. This sou

Re: Interfacing with basic C++ class

2022-09-28 Thread Ali Çehreli via Digitalmars-d-learn
On 9/28/22 12:57, Riccardo M wrote: > class MyClass { > public: > int field; > MyClass(int a) : field(a) {} Make the following function 'virtual': > int add(int asd) { virtual int add(int asd) { I think the C++ class does not get a vptr without a virtual function and appar

Interfacing with basic C++ class

2022-09-28 Thread Riccardo M via Digitalmars-d-learn
I think I am stuck in the easiest of the issues and yet it seems I cannot get around this. I have a C++ file: ``` class MyClass { public: int field; MyClass(int a) : field(a) {} int add(int asd) { return asd + 1; } }; MyClass* instantiate(int asd) { return new MyClas