x y z or float p[3]

Started by
24 comments, last by Promit 18 years, 2 months ago
Which one is better for a vector class?? I'm currently using something like

class Vector_3
{
     float x,y,z;
};
But sometimes u need an array of 3 or 4 floats for opengl calls like light colors positions etc. How do you handle this?Make it a float array?Or is there another way? I overloaded the [] operator this way

const float &operator[](int i)
{
if(i==0)
return x;
else if(i==1)
return y;
else if(i==2)
return z;

}
But I dont think its a good way O_O
Advertisement
look into unions, so you can have both.

class Vector3
{
union
{
struct
{
float x, y, z;
};
float p[3];
};
};
Just do this (for a POD it is legal)
Vector_3 vec;glFunction(&vec);
So... Muira Yoshimoto sliced off his head, walked 8 miles, and defeated a Mongolian horde... by beating them with his head?

Documentation? "We are writing games, we don't have to document anything".
What you can do is this:

class Vector4{    float x, y, z, w;    float &operator[]( int i ) const    {        return ( float & )*( &x + i );    }    operator float *()    {        return &x;    }};


This allows you to access the members using dot operators ( theVec.x ), arrays ( theVec[2] ) AND typecast it to a float pointer.
Doh yea unions.
Well unions share the same memory as far as i know.So Im not wasting any memory for it its the same as 3 floats?
And whats the purpose of the struct inside the class cant i just have :
class Vec3{public:	union{		float x,y,z;		float p[3];	};};

This seems to work.
The problem with a union is that you have to access members using something like:

theVec.p[1]

Instead of:

theVec[1]

I suppose its down to preference, but I would recommend the operator overloads above the union solution.

Edit: You can also do this:

glFunction( theVec )

No '&' symbol.
Quote:Original post by TheOddMan
What you can do is this:

*** Source Snippet Removed ***

This allows you to access the members using dot operators ( theVec.x ), arrays ( theVec[2] ) AND typecast it to a float pointer.


Watch out, this is only legal for POD types. If your x,y,z are private, have constructors, or whatever else, then it's not legal. For a discussion of an always-legal solution, see here.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
You can make the solution use private data types easily:

class Vector3{private:    float x, y, z;public:    float &X() { return x; }    const float &X() const { return x; }    float &Y() { return y; }    const float &Y() const { return y; }    float &Z() { return z; }    const float &Z() const { return z; }    float &operator[]( int i ) const    {        return ( float & )*( &x + i );    }    operator float *()    {        return &x;    }};


Just altered in my project and seems fine.
Well i have x,y,z public and they are just floats.But the Vector class has constructors is that a problem??
another problem when overloading [] is when I have a array of vectors lets say

Vector_3 *pVertices = new Vector_3[1000];

not If i want to reach the 500 vertex

pVertices[500] but this will call the overloaded [] right??What will happen?

This topic is closed to new replies.

Advertisement