Public Group

# x y z or float p[3]

This topic is 4671 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### Share on other sites
look into unions, so you can have both.

class Vector3
{
union
{
struct
{
float x, y, z;
};
float p[3];
};
};

##### Share on other sites
Just do this (for a POD it is legal)
Vector_3 vec;glFunction(&vec);

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites
The problem with a union is that you have to access members using something like:

theVec.p[1]

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.

##### Share on other sites
Quote:
 Original post by TheOddManWhat 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.

##### Share on other sites
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.

##### Share on other sites
Well i have x,y,z public and they are just floats.But the Vector class has constructors is that a problem??

##### Share on other sites
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?

1. 1
Rutin
32
2. 2
3. 3
4. 4
5. 5

• 13
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633325
• Total Posts
3011370
• ### Who's Online (See full list)

There are no registered users currently online

×