September 1998
Intermediate to advanced
848 pages
20h 13m
English
C++ is stricter about using enumerations than C is. In particular, about the only useful things you can do with an enum variable are assign an enum constant to it and compare it to other values. You can't assign ints to an enum without an explicit type cast, and you can't increment an enum variable.
enum sample {sage, thyme, salt, pepper};
enum sample season;
season = sage; /* ok in C, C++ */
season = 2; /* warning in C, error in C++ */
season = (enum sample) 3; /* ok in C, C++ */
season++; /* ok in C, error in C++ */
Also, C++ lets you drop the keyword enum when declaring a variable:
enum sample {sage, thyme, salt, pepper};
sample season; /* invalid C, valid C++ */
As was the case with structures and unions, this can lead to ...
Read now
Unlock full access