www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to do "C++ classes"?

reply rempas <rempas tutanota.com> writes:
I'm seeing in the page about "BeterC" and in the part about the 
[retained 
features](https://dlang.org/spec/betterc.html#retained
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?
Sep 18 2021
parent reply Adam D Ruppe <destructionator gmail.com> writes:
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 

 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(); }
Sep 18 2021
next sibling parent rempas <rempas tutanota.com> writes:
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 

 "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!
Sep 19 2021
prev sibling parent reply Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= <aferust gmail.com> writes:
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 

 "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.
Sep 20 2021
parent reply Adam D Ruppe <destructionator gmail.com> writes:
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.
Sep 20 2021
parent reply Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= <aferust gmail.com> writes:
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.
Sep 20 2021
parent Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= <aferust gmail.com> writes:
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.
Sep 20 2021