• Welcome to Valhalla Legends Archive.
 

Unions in C++

Started by MyndFyre, April 26, 2004, 07:47 PM

Previous topic - Next topic

MyndFyre

This is kinda for Mephisto -- since we'd talked about unions and C++ some time ago.

I was looking through the source of a Windows port of Bnetd (found on scmillenium.com), and I came across this typedef:


typedef struct bnet_packet {
 struct bnet_header header;
 union {
   char data[MAX_PACKET_SIZE-sizeof(t_header)];
   t_server_authreply server_authreply;
   t_server_cdkeyreply server_cdkeyreply;
   t_client_statsreq client_statsreq;
   t_server_statsreply server_statsreply;
   t_client_login client_login;
   t_server_loginreply server_loginreply;
   t_client_progident2 client_progident2;
   t_client_joinchannel client_joinchannel;
   t_server_serverlist server_serverlist;
   t_server_message server_message;
   t_client_gamelistreq client_gamelistreq;
   t_server_gamelist server_gamelist;
   t_client_startgame client_startgame;
   t_server_startgame_ack server_startgame_ack;
   t_server_mapauthreply server_mapauthreply;
 } u;
} t_packet;


I thought that was pretty neat.  It's a perfect example of what unions do -- only one of those types within the union will be used.

Anyway, just thought I'd share.  :)
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Mephisto

Awsome, thankyou.

Just a note, I already knew what unions were, I was just under the impression they were native to C, though I'll have to admit I wasn't as clear on them as I should've been until you pointed out my error.  :p

Maddox

Quote from: Mephisto on April 26, 2004, 08:23 PM
Awsome, thankyou.

Just a note, I already knew what unions were, I was just under the impression they were native to C, though I'll have to admit I wasn't as clear on them as I should've been until you pointed out my error.  :p

You'll find that most, if not everything native to C, is native to C++ as well...
asdf.