union wierdness
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
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).
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).
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.
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.
Quote:Original post by ZahlmanI second this approach. Keep the array one only, and use named indexes into it, instead of using named variables.typedef enum { Near, Far, Left, Right, Up, Down } Plane_Names;
KISS.
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.
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
(and no api here haha, just my own crappy fustrum culling system)
-Dan
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
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
Off-Topic:
Quote:Original post by Ademan555If 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. :-/
(oh and i know i spelled frustrum wrong in my file name :-p ill get around to changing it eventually)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement