Manifest constant class instances

Alex sascha.orlov at gmail.com
Sun Nov 4 22:06:28 UTC 2018


On Sunday, 4 November 2018 at 21:26:23 UTC, kinke wrote:
> On Sunday, 4 November 2018 at 21:08:56 UTC, kinke wrote:
>> `enum c = new C()` doesn't imply that the instance lives at 
>> runtime too.
>
> To make this point clearer: this works, but the instance 
> doesn't live at runtime:
>
> ```
> class C { int foo() { return 123; } }
> enum i = new C().foo();
>
> void main()
> {
>     import core.stdc.stdio;
>     printf("%d\n", i); // i is a 123 literal
> }
> ```

This is kind of interesting...

´´´
import std.stdio;

class C
{
	int member;

	int foo()
	{
		member++;
		return 123 + member;
	}
}

enum i = new C().foo();
enum u = &(new C().foo);

void main()
{
     import core.stdc.stdio;
     printf("%d\n", i); // 124
	writeln(u());  // 124
	writeln(i);    // 124
	writeln(u());  // 125
}
´´´


More information about the Digitalmars-d mailing list