class that is initialized becomes null when outside of function

ag0aep6g anonymous at example.com
Mon Jul 9 09:38:30 UTC 2018


On 07/09/2018 11:18 AM, Flaze07 wrote:
> class Game
> {
[...]
>      RenderWindow win;
[...]
>      void init()
>      {
[...]
>          auto win = new RenderWindow( VideoMode( 600, 600 ), "snake" );
[...]
>      }
>      void run()
>      {
[...]
>          writeln( win is null );
[...]
>      }
> }
> 
> the problem here is, during init(), the writeln will output false, which 
> means yes win is initialized, however during run(), suddenly win is null 
> becomes true...

The `win` you're creating in `init` is a function-local variable. It 
ceases to exist when `init` returns. It's not `this.win`.

In `run`, you're accessing `this.win`. It's still null because you never 
assigned anything there.

So change `auto win = ...;` to `this.win = ...;` or simply `win = ...;`.


More information about the Digitalmars-d-learn mailing list