Valhalla Legends Forums Archive | C/C++ Programming | Unions in C++

AuthorMessageTime
Myndfyr
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:

[code]
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;
[/code]

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. :)
April 27, 2004, 12:47 AM
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
April 27, 2004, 1:23 AM
Maddox
[quote author=Mephisto link=board=30;threadid=6496;start=0#msg57060 date=1083029023]
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
[/quote]

You'll find that most, if not everything native to C, is native to C++ as well...
April 27, 2004, 5:32 AM

Search