3 ms·
This is incredibly common in embedded programming. Lots of times you'll have some binary packet come over the wire, the first field is a packet type specifier,
by com2kid 13y ago
This is incredibly common in embedded programming. Lots of times you'll have some binary packet come over the wire, the first field is a packet type specifier, you can then just cast the rest of your byte buffer to the proper type.
It is very unfortunate that C (and until recently C++) did not have a way to specify the size of enums. Basically this has resulted in crappy #defines being used for far too many years.
I always called what you did above "tagged types". A number of type systems actually work like that under the covers, objects get a type ID, and then a lookup function figures out what that type ID maps to.
- jevinskie 13y agoI agree, this typed enum in C++11 made a MachO manipulation library much nicer.
- _pmf_ 13y ago> It is very unfortunate that C (and until recently C++) did not have a way to specify the size of enums. It had compiler specific switches to set the size of all enums globally, which helped exactly nobody.
- qznc 13y agoThe more common term is "tagged union". http://en.wikipedia.org/wiki/Tagged_union http://en.wikipedia.org/wiki/Tagged_union