0

I've just come across this in code and I don't understand what it's doing, or how it's doing what it's doing

pos[iter](1) += 12.0f / 900.0f;

does the (1) initialise the array value at position iter to 1, then add (12.0f / 900.0f) to it? I can't believe it is as it's being used in a loop to set the position of debug text, each loop sets the next line below to a high y value (lower point on screen). iter can be 0 or 1 depending on what list the debug text is in.

I would understand if it were a static operation.

cool mr croc
  • 725
  • 1
  • 13
  • 33

1 Answers1

5
pos[iter](1) += 12.0f / 900.0f;

pos can be an array, map, or an object of a class with an overloaded operator[]. pos[iter] returns an object (could be a function pointer, lambda or a class with an overloaded operator()) and calls it with a parameter of 1. The function call most likely returns a reference to the returned object as you can mutate its value using +=.

So to make it clear, (1) is not accessing the 2nd element of anything. It's simply a call to a function or method with that argument. The method returns some object that has a suitable overload of operator += (could be a scalar or an actual class).

Community
  • 1
  • 1
David G
  • 94,763
  • 41
  • 167
  • 253