Objects(from classes) at Compile time? no gc

Tolga Cakiroglu via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu May 15 22:55:15 PDT 2014


On Friday, 16 May 2014 at 04:59:46 UTC, Taylor Hillegeist wrote:
> The subject says it all really. i have this example:
>
> import core.memory;
>
> class fruit{
>   int value=5;
>   public int getvalue(){
>     return value;
>   }
> }
>
> int main(string[] args) {
>     GC.disable;
>     static fruit myfruit;
>     return myfruit.getvalue();
> }
>
> Most of the smart people will see that i want the program to 
> return 5 but I did something dumb and didn't put in the "new" 
> statement?
>
> So my question is in longer words "Can I create instances of 
> objects at compile time?" and if not "why not, i could build 
> something (roughly)equivalent out of structs and functions and 
> have it at compile time?"

First of all, that "static" keyword is meaningless there. You can 
remove it.

Secondly, the "getvalue" method is not defined as "static". So, 
it requires a object. It is not bound to class.

Thirdly, when you define "fruit myfruit", the variable "myfruit" 
is still "null". It doesn't point anywhere in memory. Therefore, 
the variable "value" is no where in memory. And, thereby there is 
no spoon, I mean 5.


More information about the Digitalmars-d-learn mailing list