Classes on stack

IGotD- nise at nise.com
Thu Sep 1 13:53:06 UTC 2022


On Thursday, 1 September 2022 at 09:43:55 UTC, Redwan wrote:
> On Thursday, 1 September 2022 at 09:40:06 UTC, Dennis wrote:
>> On Thursday, 1 September 2022 at 09:13:56 UTC, Redwan wrote:
>>> I want declare classes and create instance of them on stack 
>>> and nicely use them.
>>
>> You can use that using the `scope` storage class on a local 
>> variable with a class type:
>>
>> https://dlang.org/spec/attribute.html#scope-class-var
>>
>> ```D
>> void main() @nogc
>> {
>>     scope o = new Object();
>> }
>> ```
>>
>> The `@nogc` attribute ensures no allocations are made with the 
>> Garbage Collector.
>
> oh, thank you. with this, no allocation??


If you have any classes in the class you allocate on the stack, 
the constructor of the class will allocate them on the heap. 
Basically only the top level class will be on stack.


More information about the Digitalmars-d mailing list