Classes as enums in D?
Meta via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Nov 30 04:38:09 PST 2015
On Monday, 30 November 2015 at 10:22:54 UTC, Marc Schütz wrote:
> You're misinterpreting this:
>
> enum X {
> A = new Object,
> B = new Object,
> }
>
> void main() {
> import std.stdio;
> writeln(cast(void*) X.A);
> writeln(cast(void*) X.A);
> }
>
> # output:
> 470910
> 470910
>
> You're print the address of `f` and `n` on the stack, not the
> reference they're pointing to.
>
> But it's true that enums of mutable _arrays_ do create a new
> instance every time they're used:
>
> enum X {
> A = [1,2,3],
> B = [4,5,6],
> }
>
> void main() {
> import std.stdio;
> writeln(X.A.ptr);
> writeln(X.A.ptr);
> }
>
> # output:
> 7FD887F0E000
> 7FD887F0E010
Whoops, you're right. I forgot you have to cast to a pointer for
classes.
More information about the Digitalmars-d-learn
mailing list