New With Struct and Getting Class Object Pointers

rikki cattermole rikki at cattermole.co.nz
Sun Sep 30 07:47:54 UTC 2018


On 30/09/2018 8:29 PM, Vijay Nayar wrote:
> I have two brief questions.
> 
> Code that uses "new" to create struct objects appears to compile and 
> run. Is this an actual language feature, to get structs on the heap?
> 
> void main()
> {
>      struct S {int data = 1;}
>      S* s1 = new S();
>      S* s2 = s1;
>      S s3 = *s1;  // Still copies on assignment.
>      s3.data = 2;
>      assert(s1.data != s3.data);
> }

Yes. Uses a compiler hook to call into the GC, like with everything else.


More information about the Digitalmars-d-learn mailing list