What is difference between struct and class?

Mike Parker aldacron at gmail.com
Mon Jun 3 08:47:41 UTC 2019


On Monday, 3 June 2019 at 07:13:44 UTC, Rnd wrote:

>
> I know 'new' is not needed to create instances of structs but 
> can one use 'new'?

Yes. It can be used with any value type to allocate a block of 
memory on the GC heap and return a pointer to that memory:

struct Foo { ... }
Foo* f = new Foo;

int* i = new int;


>
> If yes, when should one use 'new'?

Whenever you need to allocate something from the GC heap. In my 
experience, it's rare to need it with value types in D. I tend to 
use it primarily with classes and arrays.




More information about the Digitalmars-d-learn mailing list