Jump to content
  • Advertisement
Sign in to follow this  
Richy2k

C++ and Unions

This topic is 4939 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

Check the link snk_kid posted. It took some work, but we did hammer out an acceptable solution.

And, well, if it takes rewriting significant parts of your code, you can just admit that you've been writing non-portable code. There's nothing wrong with that so long as you are aware of it and doing it deliberately.

Share this post


Link to post
Share on other sites
Advertisement
also, if you dont mind 12 extra bytes on each vector, you can do:

class Vector
{
public:
Vector():x(v[0]),y(v[1]),z(v[2]) {};
float v[3];
float& x;
float& y;
float& z;
};



Then you can access the values with either v[] or x,y,z.

Share this post


Link to post
Share on other sites
lucky for me i'm phasing out using X Y and Z...any code i'm writing from now on wont be using those.

PS: Tried in GCC on an old knoppix disc, anon unions and structs work.

Share this post


Link to post
Share on other sites
Quote:
Original post by Kwizatz
also, if you dont mind 12 extra bytes on each vector, you can do:
*** Source Snippet Removed ***

Then you can access the values with either v[] or x,y,z.


or you could make your membervariables nonpublic and write the []operator and a memberfunction called 'x' another called 'y' and a third called 'z' omg... then you could call it like "Vector pie; pie.x()= pie.y();".

code:

class Vector{
Scalar i[3];
Scalar& X(){return i[0];};
public:
//fobar...
};


if you hate the function syntax for membervariables you could write it this way:
code:

class Vector{
Scalar x,y,z;
Scalar& operator[](int i){switch(i){case 0: return x; /*...*/};};
public:
//fobar...
};


that way you can use it like "Vector pie; pie.x= pie[2];" ... but only in friend or memberfunctions, or you will have to make the membervariables and the operator public


T2k

Share this post


Link to post
Share on other sites
btw, just look at that code and think for yourself:


class Vector{
public:
union{
SCALAR i[1];
SCALAR x;
};
SCALAR y;
SCALAR z;
};



thats in case you dont want to allow component access with the [] operator directly but through the i member: "Vector foo; foo.i[2]= foo.z;"


T2k

Share this post


Link to post
Share on other sites
Anonymous unions are part of standard C++.

I forget whether anonymous structs are, but they're certainly portable between both MSVC and GCC, which covers all platforms you're likely to encounter for a long time.

Specifically, this code compiles without complaints on both GCC and MSVC:


#include <stdio.h>

struct Vert {
float d[3];
struct {
float x, y, z;
};
};

int main() {
Vert v;
v.d[0] = 0;
v.y = 1;
return 0;
}

Share this post


Link to post
Share on other sites
I see this one hasn't come up yet:

struct vector3d {
typedef ... scalar_type;
scalar_type x,y,z;
private:
const static scalar_type vector3d::*element[] = { &vector3d::x , &vector3d::y , &vector3d::z };
public:
scalar_type & operator[]( size_t index ) {
return this->*element[ index ];
}
const scalar_type & operator[]( size_t index ) const {
return this->*element[ index ];
}
};

vector3d foo;
foo.x = 13;
assert( foo[0] == 13 );

Share this post


Link to post
Share on other sites
I still think it's weird that you're allowed to have a reference to a member datum in a static context o_O

The switch-based operator[] presented by T2K appeals to me in that it's easier to understand and allows for bounds checking (you could throw an exception in the default case), but it's that much more explicit code to write, and you need to duplicate it all in order to provide the const version :(

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!