Thread overview
How to do "C++ classes"?
Sep 18, 2021
rempas
Sep 18, 2021
Adam D Ruppe
Sep 19, 2021
rempas
Sep 20, 2021
Ferhat Kurtulmuş
Sep 20, 2021
Adam D Ruppe
Sep 20, 2021
Ferhat Kurtulmuş
Sep 20, 2021
Ferhat Kurtulmuş
September 18, 2021

I'm seeing in the page about "BeterC" and in the part about the retained features, the #11 says about "COM classes and C++ classes". What are the "C++ classes"? I tried to create a class using "extern(C++)" but this didn't worked. Can someone make an example on that?

September 18, 2021
On Saturday, 18 September 2021 at 15:38:38 UTC, rempas wrote:
> I'm seeing in the page about "BeterC" and in the part about the [retained features](https://dlang.org/spec/betterc.html#retained), the #11 says about "COM classes and C++ classes". What are the "C++ classes"? I tried to create a class using "extern(C++)" but this didn't worked. Can someone make an example on that?

extern(C++)
class Foo {}

void main() {
    scope Foo foo = new Foo();
}


September 19, 2021
On Saturday, 18 September 2021 at 22:16:32 UTC, Adam D Ruppe wrote:
> On Saturday, 18 September 2021 at 15:38:38 UTC, rempas wrote:
>> I'm seeing in the page about "BeterC" and in the part about the [retained features](https://dlang.org/spec/betterc.html#retained), the #11 says about "COM classes and C++ classes". What are the "C++ classes"? I tried to create a class using "extern(C++)" but this didn't worked. Can someone make an example on that?
>
> extern(C++)
> class Foo {}
>
> void main() {
>     scope Foo foo = new Foo();
> }

Works flawlessly! Thanks a lot!
September 20, 2021

On Saturday, 18 September 2021 at 22:16:32 UTC, Adam D Ruppe wrote:

>

On Saturday, 18 September 2021 at 15:38:38 UTC, rempas wrote:

>

I'm seeing in the page about "BeterC" and in the part about the retained features, the #11 says about "COM classes and C++ classes". What are the "C++ classes"? I tried to create a class using "extern(C++)" but this didn't worked. Can someone make an example on that?

extern(C++)
class Foo {}

void main() {
scope Foo foo = new Foo();
}

I thought it's stack-allocated and scoped. But when I try to return a class instance from a function, it still works? Captain Adam I need an explanation please.

September 20, 2021
On Monday, 20 September 2021 at 15:35:02 UTC, Ferhat Kurtulmuş wrote:
> I thought it's stack-allocated and scoped.

It is.

> But when I try to return a class instance from a function, it still works?

dmd only makes that an error if you specify `@safe` and i think `-dip1000`. Try adding one or both of those and recompiling and see what happens.

Note that even if the compiler doesn't error on it, it is undefined behavior to return the stack reference so be sure to treat it right.
September 20, 2021
On Monday, 20 September 2021 at 15:45:08 UTC, Adam D Ruppe wrote:
> On Monday, 20 September 2021 at 15:35:02 UTC, Ferhat Kurtulmuş wrote:
>> I thought it's stack-allocated and scoped.
>
> It is.
>
>> But when I try to return a class instance from a function, it still works?
>
> dmd only makes that an error if you specify `@safe` and i think `-dip1000`. Try adding one or both of those and recompiling and see what happens.
>
> Note that even if the compiler doesn't error on it, it is undefined behavior to return the stack reference so be sure to treat it right.

That is what I thought too. I only tried this on the online compiler. Thank you. Have a great day or night captain.
September 20, 2021
On Monday, 20 September 2021 at 15:56:44 UTC, Ferhat Kurtulmuş wrote:
> On Monday, 20 September 2021 at 15:45:08 UTC, Adam D Ruppe wrote:
>> On Monday, 20 September 2021 at 15:35:02 UTC, Ferhat Kurtulmuş wrote:
>>> I thought it's stack-allocated and scoped.
>>
>> It is.
>>
>>> But when I try to return a class instance from a function, it still works?
>>
>> dmd only makes that an error if you specify `@safe` and i think `-dip1000`. Try adding one or both of those and recompiling and see what happens.
>>
>> Note that even if the compiler doesn't error on it, it is undefined behavior to return the stack reference so be sure to treat it right.
>
> That is what I thought too. I only tried this on the online compiler. Thank you. Have a great day or night captain.

I also think this is a dirty corner of the complier since it must raise an error for scoped instances of classes.