Enumerations

I l @ ve RuBoard

Enumerations

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 int s 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 conflicts if a variable and an enum type have the same name .

I l @ ve RuBoard


C++ Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 314
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net