enums

John Danskin jmd at granite.dec.com
Thu Jul 21 02:45:44 AEST 1988


In article <469 at m3.mfci.UUCP> karzes at 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.



More information about the Comp.std.c mailing list