Complete.Org: Mailing Lists: Archives: freeciv-dev: June 1999:
Re: [Freeciv-Dev] Programing style
Home

Re: [Freeciv-Dev] Programing style

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: Freeciv developers <freeciv-dev@xxxxxxxxxxx>
Subject: Re: [Freeciv-Dev] Programing style
From: Chris Lawrence <cnlawren@xxxxxxxxxxx>
Date: Thu, 10 Jun 1999 19:28:20 -0500
Reply-to: Chris Lawrence <cnlawren@xxxxxxxxxxx>

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

[Prev in Thread] Current Thread [Next in Thread]