Article View: comp.std.c
Article #5216Re: enums
From: John Danskin
Date: Wed, 20 Jul 1988 16:45
Date: Wed, 20 Jul 1988 16:45
60 lines
1820 bytes
1820 bytes
In article <469@m3.mfci.UUCP> karzes@mfci.UUCP (Tom Karzes) writes:
>While on the subject of enums, here's something that's always bothered me.
>For example:
> enum tree {
> oak,
...
> spruce
> };
>
>Now I'd like to define TREE_COUNT to be the number of trees. So I have to
>painfully count them, and write:
> #define TREE_COUNT 7
>
>and hope that people correctly update this macro every time they add or
>delete a tree.
>
>Sure, I suppose in this case I could write:
>
> #define TREE_COUNT ((int) spruce + 1)
>
>(This assumes that there aren't any trees which have been given explicit
>values in the enum.) But this is still a pain to maintain, since you
>have to fix the macro every time the last tree changes.
Why don't you just write:
enum tree {
oak,
elm,
maple,
birch,
willow,
cypress,
spruce,
NUM_TREES
};
#define TREE_COUNT ((int)NUM_TREES)
?
You still have a problem with enums with assigned values, but hey, if you
are assigning your own values to enums you had better go have a look at
all of your macros every time you change anything anyway.
--
John Danskin | decwrl!jmd
DEC Technology Development | (415) 853-6724
100 Hamilton Avenue | My comments are my own.
Palo Alto, CA 94306 | I do not speak for DEC.
Technical Details
Message-ID:
<250@granite.dec.com>Path:
arc1-ca.pugleaf.net!utzoo!attcan!uunet!lll-winken!lll-tis!helios.ee.lbl.gov!pasteur!ucbvax!decwrl!granite!jmdReferences:
<1608@dataio.Data-IO.COM> <469@m3.mfci.UUCP>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!lll-winken!lll-tis!helios.ee.lbl.gov!pasteur!ucbvax!decwrl!granite!jmd From: jmd@granite.dec.com (John Danskin) Newsgroups: comp.std.c Subject: Re: enums Message-ID: <250@granite.dec.com> Date: 20 Jul 88 16:45:44 GMT References: <1608@dataio.Data-IO.COM> <469@m3.mfci.UUCP> Reply-To: jmd@granite.UUCP (John Danskin) Organization: DEC Technology Development, Palo Alto, CA Lines: 59