Sign in to follow this  

A few questions...

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

I know I haven't posted here in a long while... But I had a big move bout 4 months ago and I got my internet back only 2 weeks or so ago. I have 2 questions here: 1) Could someone explain to me, in depth, what a union is and is for? 2) Could someone point me somewhere where I can learn more about developing a script system for my game? Thanks in advance, -- RH

Share this post


Link to post
Share on other sites
1)A union (in C and C++) allows a programmer to reinterprete data. The primary benefit of this is to save space. If you had a function and you wanted to return either type A or type B you could use a(n anonymous) union like so:

struct A
{
char data[10];
};

struct B
{
int data[10];
};
enum TYPES {
TYPE_A,
TYPE_B
};

struct TypeUnion{
TYPES type
union {
A a;
B b;
};
};

TypeUnion getData();

int main()
{
TypeUnion temp = getData();
if(temp.type == TYPE_A)
{
// print char data
}
else if(temp.type == TYPE_B)
{
// print int data
}
else
{
// error, invalid type
}
}


Unions are tools and if you see a situation where you can use one, go right ahead. One caveat: I think unions only work with Plain-Old Data (google it!)

Share this post


Link to post
Share on other sites

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