Classes on stack

IGotD- nise at nise.com
Thu Sep 1 19:45:08 UTC 2022


On Thursday, 1 September 2022 at 19:27:13 UTC, Paul Backus wrote:
>
> 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
> }

I previously saw an example that allocated the space in the class 
for the member class, unfortunately I cannot find it. Then it 
runs emplace in order to run the constructor. This way you don't 
need to allocate it outside the top level stack, which is 
annoying when you have many of them.


More information about the Digitalmars-d mailing list