union wierdness

Started by
7 comments, last by Agony 19 years, 5 months ago
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
When General Patton died after World War 2 he went to the gates of Heaven to talk to St. Peter. The first thing he asked is if there were any Marines in heaven. St. Peter told him no, Marines are too rowdy for heaven. He then asked why Patton wanted to know. Patton told him he was sick of the Marines overshadowing the Army because they did more with less and were all hard-core sons of bitches. St. Peter reassured him there were no Marines so Patton went into Heaven. As he was checking out his new home he rounded a corner and saw someone in Marine Dress Blues. He ran back to St. Peter and yelled "You lied to me! There are Marines in heaven!" St. Peter said "Who him? That's just God. He wishes he were a Marine."
Advertisement
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).
*bawls*
*sniffle*
well i might try the workaround

thanks
-Dan
When General Patton died after World War 2 he went to the gates of Heaven to talk to St. Peter. The first thing he asked is if there were any Marines in heaven. St. Peter told him no, Marines are too rowdy for heaven. He then asked why Patton wanted to know. Patton told him he was sick of the Marines overshadowing the Army because they did more with less and were all hard-core sons of bitches. St. Peter reassured him there were no Marines so Patton went into Heaven. As he was checking out his new home he rounded a corner and saw someone in Marine Dress Blues. He ran back to St. Peter and yelled "You lied to me! There are Marines in heaven!" St. Peter said "Who him? That's just God. He wishes he were a Marine."
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.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
typedef enum { Near, Far, Left, Right, Up, Down } Plane_Names;


KISS.
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.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
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
When General Patton died after World War 2 he went to the gates of Heaven to talk to St. Peter. The first thing he asked is if there were any Marines in heaven. St. Peter told him no, Marines are too rowdy for heaven. He then asked why Patton wanted to know. Patton told him he was sick of the Marines overshadowing the Army because they did more with less and were all hard-core sons of bitches. St. Peter reassured him there were no Marines so Patton went into Heaven. As he was checking out his new home he rounded a corner and saw someone in Marine Dress Blues. He ran back to St. Peter and yelled "You lied to me! There are Marines in heaven!" St. Peter said "Who him? That's just God. He wishes he were a Marine."
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
CheersChris
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. :-/
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke

This topic is closed to new replies.

Advertisement