Whats the best way of doing this?

Started by
7 comments, last by snk_kid 17 years, 7 months ago
Hey, I have a Vector class which looks something like this:


class Vec3 {
public:
     float x, y, z;

public:
     ///operators, methods go here

    ...
}

This is of course pretty standard, now rather than supplying xyz to glVertex calls etc. I want to use the glVertex3fv() array counterparts functions. I was wondering if someone knew the best way to write a method that would return the values in the correct format for the 3fv methods. I thought perhaps returning a reference to x might work but im not sure its a good way of doing it, do all compilers store the variables one after the other in memory? I'll be honest, I know I'm being slightly lazy just asking here when I could probably work this out myself, but I flew home from Portugal last nite and im knackered :) so thanks in advance to anyone that replies, Cheers Luke.
Member of the NeHe team.
Advertisement
Floats are 4-bytes long, so on any relatively sane platform they will be tightly packed. So yes, declare an operator float () which returns a pointer to x, and it will be called automatically:
class vector{public:	float x, y, z;public:	operator float *() {return &x;}};vector v1;glVertex3fv(v1);

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Another way is to use non-standard extensions as the nameless union:
class Vector3{public:  union  {    // A struct makes sure x, y and z don't share the same memory of the union    struct    {       float m_x, m_y, m_z;    }    // The array uses the same memory as the struct    float m_elements[3];  }};


This is what I use on MS Visual C++. Greetz,

Illco
If you use swifcoder's code, you should at least make them GLfloats instead of floats, because GLfloats are guaranteed to be four bytes. If it was me though, I would just store the x, y, and z in an array.
Quote:Original post by Illco
Another way is to use non-standard extensions as the nameless union...


Or use the standard compliant & portable solution
Cool thanks guys!

Rate++ to all :D
Member of the NeHe team.
Quote:Original post by Simian Man
If you use swifcoder's code, you should at least make them GLfloats instead of floats, because GLfloats are guaranteed to be four bytes. If it was me though, I would just store the x, y, and z in an array.


If they are IEEE floats then they are 4 bytes, so I would not worry about the above quote.

As for swiftcoder's code I would add another operator
operator float const*() {return &x;}

Quote:Original post by snk_kid
Quote:Original post by Illco
Another way is to use non-standard extensions as the nameless union...


Or use the standard compliant & portable solution


By Gosh! Brilliant!
Quote:Original post by Anonymous Poster
As for swiftcoder's code I would add another operator
*** Source Snippet Removed ***


i'd prefer:

struct vector {   float x, y, z;   typedef float (&array_ref)[3];   typedef const float (&const_array_ref)[3];   //....   operator array_ref() {       return reinterpret_cast<array_ref>(*this);   }   operator const_array_ref() const {       return reinterpret_cast<const_array_ref>(*this);   }};


But that is being abit too pedantic.

This topic is closed to new replies.

Advertisement