Jump to content
  • Advertisement
Sign in to follow this  
RavynousHunter

A few questions...

This topic is 3747 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!