hi all,
i am creating (again) a math library for my projects and I have some design questions
until now I had a vector class like this
class cVector3
{
GLfloat x,y,z;
cVector3() : x(0),y(0),z(0) {}
// useful functions here
float length();
void normalize();
cVector3 cross(cVector3& v) const;
.... etc .....
// operators
cVector3 operator + (cVector3& v) const
{
return cVector3(x+v.x, y+v.y, z+v.z);
}
.... same for subtraction etc
}
Now, obviously I want my vector class to be super-fast and light
I've realized that the use of overloaded operators like the one above, although makes the code more readable, is inefficient because it creates a temporary vector to store the result and return it. We are talking about an operation that will be applied thousands of times every frame (but you already know that, don't you?)
So should I imlement a function like
void AddVectors(const cVector3& v1, const cVector3& v2, cVector3 &result);
or
class cVector3
{
//...
// store to (*this) the sum of v1 and v2
void AddVectors(const cVector3& v1, const cVector3& v2)
{
this->x = v1.x+v2.x;
this->y = v1.y+v2.y;
this->z = v1.z+v2.z;
}
//...
}
or
class cVector3
{
//....
static void AddVectors(const cVector3& v1,
const cVector3& v2,
cVector3 &result);
//...
}
I think that the last one is more preferable (organization-wise). But, does the member functions of a class add any overhead? if a model consists of 5000 edges, would there be any performance issue? Should I keep the class empty (with only x,y,z) and make every function outside any class (like the first one)?
Inheritance is not an issue, I will not derive any class from cVector3
What do you think?