Valhalla Legends Archive

Programming => General Programming => C/C++ Programming => Topic started by: MyndFyre on April 26, 2004, 07:47 PM

Title: Unions in C++
Post by: MyndFyre on April 26, 2004, 07:47 PM
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.  :)
Title: Re:Unions in C++
Post by: 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
Title: Re:Unions in C++
Post by: Maddox on April 27, 2004, 12:32 AM
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...