Jump to content
  • Advertisement
Sign in to follow this  
Black Knight

x y z or float p[3]

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
look into unions, so you can have both.

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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!