Are stack+heap classes possible in D?

Adam D. Ruppe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jun 13 18:31:24 PDT 2015


On Sunday, 14 June 2015 at 00:52:20 UTC, FujiBar wrote:
> I have read that in D structs are always allocated on the stack 
> while classes are always allocated on the heap.

That's not true; it is a really common misconception.

Putting a struct on the heap is trivial and built into the 
language: `S* s = new S();`

Putting a class on the stack is a bit trickier, but the standard 
library provides a helper to do it: 
http://dlang.org/phobos/std_typecons.html#scoped follow the 
examples given there.


More information about the Digitalmars-d-learn mailing list