data structures

Started by
4 comments, last by alargeduck 22 years, 1 month ago
Ive seen in some places a point data structure that is defined as the union of a float v[3] and float x, y, z;. What I mean is, the X coordinate can be accessed as either pt.x or pt.v[0], y as either pt.y or pt.v[2], and so on. The advatage ofcourse is readability, while still having the ability to use glVertex3fv and the other vector functions. Of course, it seems to be a general rule that when youre searching for something online that youve seen previously, you never find it. So I have no clue what the syntax is for the definition. I tried this: typedef struct t_point { union { struct { float x, y, z; }; float v[3]; }; } POINT; but that didnt work (I wouldnt be posting this if it did).
Advertisement
union t_point
{
float x,y,z;
float v[3];
} POINT;

You should get yourself a good C++ book.
or even a bad one. I picked up a US$20 reference book a couple of weeks ago. It was an absolute score. I dont have it with me here, but it''s yellow and about the size of a 500page novel and it covers pretty much anything you''d ever want regarding c/c++. Brilliant stuff.
some logical stuff : (arrays)

float v[3] ==
float* v = new float[3];

since pointers are always the size of 4, why should we refer
to a variable with the same size ???

or did I misunderstood something ?

I''m always using

float x,y,z;

because when you use arrays you have double (or more) referencing in a typedef !!!
I hate signatures !!!
struct Point { union {  struct { float x,y,z; };  float v[3]; };}; 


this works for me, dunno your problem
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia

My Page davepermen.net | My Music on Bandcamp and on Soundcloud

I'm using a casting operator to solve the problem.
struct V3f {  GLfloat x, y, z;  inline operator GLfloat*() {    return &x  }}; 

Makes it possible to write code like this:
V3f v;// Access the individual elementsv.x = v.y = v.z = 0.5;// Use it as a vectorglVertex3fv( v ); 

Don't know if there are any performance or compiler issues with this usage though.

[edited by - nadam on March 18, 2002 5:23:14 PM]

This topic is closed to new replies.

Advertisement