Sign in to follow this  

union wierdness

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

Well, i have this nasty habit of using unions in my classes, like my vectors, so you can iterate, as well as access x, y, z style, well... i did it again... only this time... with a class union { CPlane Planes[6]; struct { CPlane Near; CPlane Far; CPlane Left; CPlane Right; CPlane Up; CPlane Down; }; }; and ive gotten a nice little error that tells me (oh and i know i spelled frustrum wrong in my file name :-p ill get around to changing it eventually) c:\Documents and Settings\***\My Documents\Visual Studio Projects\Metal Flare\Fustrum.h(61) : error C2620: member 'CFrustrum::__unnamed::Planes' of union 'CFrustrum::__unnamed' has user-defined constructor or non-trivial default constructor is there any way to make this work? i could name the struct containing all of the named planes.... but that would kinda defeat the purpose of waht im doing (ease of use). could i have a constructor anyways? and call it __unnamed ? i dunno... :-/ thanks -Dan

Share this post


Link to post
Share on other sites
Long story short: the C++ standard forbids the use of objects with constructors/destructors inside of unions. What you're doing is non-kosher.

One work around for what you seem to be trying to do is Fruny's evil array of member pointer trick as explained in this thread (about halfway through).

Share this post


Link to post
Share on other sites
Why do you need unnamed planes *and* named planes?

If you're passing it to an API expecting the array of planes, then just put #pragma pack(1) around the sucker and reinterpret_cast it to be an array of planes when necessary.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
typedef enum { Near, Far, Left, Right, Up, Down } Plane_Names;


KISS.
I second this approach. Keep the array one only, and use named indexes into it, instead of using named variables.

A co-worker just made a similiar mistake a few weeks back. 8 fixed items of the same type but he didn't use an array. Needless to say the code is longer than it should be.

Share this post


Link to post
Share on other sites
The only problem with the named indices is your liable to get name collisions... which is not something im up for... so i just settled for an unnamed array...
(and no api here haha, just my own crappy fustrum culling system)

-Dan

Share this post


Link to post
Share on other sites
Just to elaborate on why you can't use types with Construcutors\destructors is that the union stores only enough memory to hold the largest member of itself and it has no knowledge of what type it actually holds.

If you've used Variants you'll know it has a vt member to hold the type of data it represents.

When the union goes out of scope if one or more of the types has a destructor the union has no idea what destructor to call if any:)

So the rule that only data types with no cstr or dstr's

Cheers
Chris

Share this post


Link to post
Share on other sites
Off-Topic:
Quote:
Original post by Ademan555
(oh and i know i spelled frustrum wrong in my file name :-p ill get around to changing it eventually)
If you do get around to changing it sometime, it's "frustum", not "frustrum". I suppose that would require changing all of your class names too, though. :-/

Share this post


Link to post
Share on other sites

This topic is 4774 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this