Re: What's the correct way of creating an instance of class in D?

2022-11-03 Thread Steven Schveighoffer via Digitalmars-d-learn
On 11/3/22 1:46 PM, Tejas wrote: Check my post, `A& a;` refuses to compile in C++20 atleast, asking to be explicitly initialized, thus averting the problem altogether That's different, `A&` cannot be rebound in C++, whereas a class reference can. Try `A* a;` and see if it compiles -Steve

Re: What's the correct way of creating an instance of class in D?

2022-11-03 Thread Tejas via Digitalmars-d-learn
On Thursday, 3 November 2022 at 15:40:02 UTC, H. S. Teoh wrote: On Thu, Nov 03, 2022 at 04:41:14AM +, Siarhei Siamashka via Digitalmars-d-learn wrote: [...] ```D @safe: import std.stdio; class A { void foo() { writeln("foo"); } } void main() { auto a1 = new A; a1.foo(); // prints

Re: What's the correct way of creating an instance of class in D?

2022-11-03 Thread Ali Çehreli via Digitalmars-d-learn
On 11/3/22 08:40, H. S. Teoh wrote: > D does not have the equivalent of C++'s allocating a class instance on > the stack. Not by default but we have two different ways, either may be discouraged: import std.typecons : scoped; import std.stdio; class C { int i; } void main() { //

Re: What's the correct way of creating an instance of class in D?

2022-11-03 Thread Adam D Ruppe via Digitalmars-d-learn
On Thursday, 3 November 2022 at 15:40:02 UTC, H. S. Teoh wrote: D does not have the equivalent of C++'s allocating a class instance on the stack. In D, all class instances are allocated on the heap and class variables are references to them. well there is scope Object o = new Object; which

Re: What's the correct way of creating an instance of class in D?

2022-11-03 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Nov 03, 2022 at 04:41:14AM +, Siarhei Siamashka via Digitalmars-d-learn wrote: [...] > ```D > @safe: > import std.stdio; > class A { > void foo() { writeln("foo"); } > } > void main() { > auto a1 = new A; > a1.foo(); // prints "foo" > A a2; > a2.foo(); // Segmentation fault

Re: What's the correct way of creating an instance of class in D?

2022-11-03 Thread Tejas via Digitalmars-d-learn
On Thursday, 3 November 2022 at 04:41:14 UTC, Siarhei Siamashka wrote: C++ code: ```C++ #include class A { public: void foo() { std::cout << "foo" << std::endl; } }; int main() { auto a1 = new A; a1->foo(); // prints "foo" A a2; a2.foo(); // prints "foo" delete a1; } ``` D code:

Re: What's the correct way of creating an instance of class in D?

2022-11-03 Thread Mike Parker via Digitalmars-d-learn
On Thursday, 3 November 2022 at 06:02:13 UTC, Mike Parker wrote: are in C++. D enforces the distinction that C++ ...that C++ programmers often follow by convention.

Re: What's the correct way of creating an instance of class in D?

2022-11-03 Thread Mike Parker via Digitalmars-d-learn
On Thursday, 3 November 2022 at 05:41:06 UTC, Siarhei Siamashka wrote: Thanks for the link and also thanks for confirming that you have no clue what's going on. I think that what actually That's not necessary. He does know what's going on and pointed you to the correct place. The second

Re: What's the correct way of creating an instance of class in D?

2022-11-02 Thread Siarhei Siamashka via Digitalmars-d-learn
On Thursday, 3 November 2022 at 05:10:06 UTC, matheus wrote: https://dlang.org/spec/class.html Thanks for the link and also thanks for confirming that you have no clue what's going on. I think that what actually happens is that the D code ```D A a2; a2.foo(); ``` is roughly equivalent

Re: What's the correct way of creating an instance of class in D?

2022-11-02 Thread matheus via Digitalmars-d-learn
On Thursday, 3 November 2022 at 04:41:14 UTC, Siarhei Siamashka wrote: ... https://dlang.org/spec/class.html Matheus.