(Maybe) Strange Behaviour of Field Initialization

eXodiquas exodiquas at gmail.com
Wed Apr 28 15:09:36 UTC 2021


Hello everyone,

I am playing around with DSFML and drawing some stuff on the 
screen. It works like a charm but I got some unexpected behavior 
when building a `Particle` class.

My class looks like this:

```d
class Particle : Drawable
{
   CircleShape shape = new CircleShape(5);

   this(int x, int y)
   {
     this.shape.position = Vector2f(x, y);
     this.shape.fillColor = Color.Green;
   }

   void draw(RenderTarget renderTarget, RenderStates renderStates) 
{
     renderTarget.draw(this.shape);
   }
}

```

When I create an array of `Particle`s and try to draw them, all 
of them are drawn on the exact same location in my window.

However, when I assign `shape` in the constructor with 
`this.shape = new CircleShape(5);` it works as expected.

The last time I wrote something in D is a few months back, but I 
cannot remember this behavior. In the code above `shape` acts 
like as if it were `static` without being `static`, obviously. Is 
there something wrong with D in this case, or is there something 
wrong with DSFML or am I just stupid right now and not able to 
see the obvious.

Thanks in advance.


More information about the Digitalmars-d-learn mailing list