I had following defines in a header:
#define Vector3_0 Vector3(0.0,0.0,0.0)
#define Vector3_x Vector3(1.0,0.0,0.0)
#define Vector3_y Vector3(0.0,1.0,0.0)
#define Vector3_z Vector3(0.0,0.0,1.0)
Avoiding macro's is a good thing (tm) plus it's always calling the constructor if you use it, so I changed it into:
In the header:
extern const Vector3 Vector3_0;
extern const Vector3 Vector3_x;
extern const Vector3 Vector3_y;
extern const Vector3 Vector3_z;
In a .cpp file:
const Vector3 Vector3_0 = Vector3(0.0,0.0,0.0);
const Vector3 Vector3_x = Vector3(1.0,0.0,0.0);
const Vector3 Vector3_y = Vector3(0.0,1.0,0.0);
const Vector3 Vector3_z = Vector3(0.0,0.0,1.0);
I had done that together with multiple changes so it wasn't immediatly clear that this could be the cause of crashes.
Anyway after that my game suddenly had a crash, and a velocity that became "NaN" instead of a velocity. It took a long time to track down, but eventually I found out that some other objects were also being created on the heap before main() was called.
And the order of that isn't defined, and something that was using Vector3_x & co was already being called before Vector3_x & co themselves had been created.
This unlike the #defines!
What should I do in this case? Go back to the #defines? Or avoid creating objects on the heap before main() is called? Does there exist another alternative to the #defines that is less dangerous? Maybe make them static?
[Edited by - Lode on March 6, 2008 7:17:42 AM]