Classes on stack
Paul Backus
snarwin at gmail.com
Thu Sep 1 19:27:13 UTC 2022
On Thursday, 1 September 2022 at 18:50:05 UTC, Redwan wrote:
> On Thursday, 1 September 2022 at 18:26:12 UTC, Ali Çehreli
> wrote:
>> import std.stdio;
>>
>> class A {
>> int i;
>> }
>>
>> class B {
>> A a;
>> int i;
>>
>> this() {
>> this.a = new A();
>> }
>> }
>>
>> void main() {
>> scope b = new B();
>> int i;
>>
>> writeln("on stack: ", &i);
>> writeln("on stack: ", &b.i);
>> writeln("on heap : ", &b.a.i);
>> }
>>
>> Ali
>
> how about `scope this.a = new A();`?
If you want everything on the stack, you have to pass in `a` as
an argument, like this:
class A {}
class B {
A a;
this(A a) {
this.a = a;
}
}
void main() {
scope a = new A(); // on stack
scope b = new B(a); // on stack
}
More information about the Digitalmars-d
mailing list