charlie@genrad.com (Charlie D. Havener) (07/03/90)
// enum.cxx - experiment with overloading << for enums // Charlie Havener - GenRad Inc. charlie@genrad.com // Both Zortech 2.06, 2.1 and Borland Turbo C++ Get this wrong! #include <stream.h> // must be iostream.h for Borland Turbo C++ enum color {SKIPZERO,RED,BLUE,GREEN}; ostream& operator<<(ostream& os,color tint); main() { int k = 99; cout << (color)BLUE << "\n"; // fails because cout is ostream_withassign // supposedly fixed in AT&T 2.1 // next line will actually ouput text strings for RED etc // except Zortech complains at compile time and Borland // fails at run time, it merely puts out integers cout << k << " " << RED << " " << BLUE << RED << GREEN << "\n"; cout << GREEN << "\n"; // fails because cout is ostream_withassign } ostream& operator<<(ostream& os,color tint) { switch ( tint ) { case RED: cout << "RED"; break; case BLUE: cout << "BLUE"; break; case GREEN: cout << "GREEN"; break; case SKIPZERO: default: cout << "color?"; } return os; } /* sample output from Sun C++ ( AT&T cfront 2.0 ) 2 99 RED BLUEREDGREEN 3 */