C_guy wrote:
Quote:
|
Thanks for the reply Rick.
|
(Wasn't his name Jensen Somers?)
Quote:
Does anyone know if I can make "myEnum_t" a 2-byte type WITHOUT the
"enumeration" feature? In other words, enum1/enum2/enum3/enumN will
all be set equal to some value, but the type myEnum_t will be a 2-byte
type...
|
"What Jensen Somers said." In other words: No, not unless the
compiler has special options/pragmas/whatever to control it.
However, it may not be all that big a deal. Remember that C's
enumerated types are really just integers with funny names and with
compiler-selected underlying types. Remember, too, that the named
values are just `int' constants, like `1' or `42' spelled strangely.
Since they are `int' constants, you can use them with any integer
type that can accommodate their values:
enum Field { WINKEN, BLINKEN, NOD };
char c = WINKEN;
short s = BLINKEN;
int i = NOD;
Also, since the enumerated type itself is simply an integer (of
some kind), variables of that type can be assigned values that are
not in the enumerated list:
enum Field f = BLINKEN + 41;
enum Field g = NOD * 3.14159; /* same as ... g = 6; */
The upshot is that if you really want a two-byte enumeration, you
give up almost nothing by choosing a plain two-byte integer and
using it to store the enumerated values. Thus, the problem reduces
to finding a two-byte integer -- still not trivial, but more easily
approached than trying to control the "width" of the enumerated
type itself.
The "almost nothing" is not exactly "nothing;" you do lose a
little bit by using a plain integer. If you `switch' on an enumerated
type and the cases do not cover all the enumerated values, as in
enum Field h = ...;
switch (h) {
case WINKEN: ...; break;
case NOD: ...; break;
}
.... some compilers will warn you that a case appears to be missing,
and the warning may help you detect an oversight in your code. But
if you `switch' on an ordinary integer that happens to hold an
enumerated value
int i = BLINKEN;
switch (i) {
case WINKEN: ...; break;
case NOD: ...; break;
}
.... the compiler will probably not issue such a warning. You could
write `switch ( (enum Field) i )' in hopes of re-enabling the warning,
but that's a difficult discipline to enforce in a big project.
Face it: C's enumerated types are not as "strong" as one might
wish them to be. Too bad, but that's the way it is.
--
Eric.Sosman@sun.com