Re: [Freeciv-Dev] Programing style
[Top] [All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
On Jun 11, Reinier Post wrote:
> The problem I encountered when I tried that approach (for a patch
> to introduce city build queues) was that C doesn't allow union values
> to be distinquished by type. This
>
> union build_item {
> enum unit_type unit;
> enum improvement_type improvement;
> };
>
> is useless because it's impossible to tell whether a build_item is
> actually a unit or a improvement. At least, that's what K&R told me.
The usual approach is:
struct build_item {
union bi_thing {
enum unit_type unit;
enum improvement_type improvement;
} bi;
int bi_type;
}
You set bi_type to say TYPE_UNIT or TYPE_IMPROVEMENT, then access the
thing through the union inside the struct using conditional code.
Yes, I know this sucks. If it's any consolation, unions almost always
work this way in languages without runtime type identification (and
languages with RTTI use more or less the C hack, you just don't have
to look at it).
Chris
|
|