What's the correct way of creating an instance of class in D?
Ali Çehreli
acehreli at yahoo.com
Thu Nov 3 16:00:26 UTC 2022
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() {
// Either this:
// auto c = scoped!C();
// Or this:
scope c = new C();
writeln("If the address values are close, then the object is on the
stack:");
writeln(&c);
writeln(&c.i);
}
Ali
More information about the Digitalmars-d-learn
mailing list