Anonymous Enumeration to Stream

Started by
5 comments, last by dmounty 20 years, 6 months ago
I want to overload the standard << operator, to let me send anonymous enumeration types to an ostream. I tried: std::ostream& operator<<( std::ostream&, const enum& ) but it said it gives me a parse error, any help would be appreciated (before anyone suggests it, yes I have to use anonymous enumeration as the type is not part of my code, so switching to standard enumeration types is not an option). Thanks in advance, Dan
Advertisement
C++ will convert enumerations to integers; you don''t need a custom << operator.
afaik, enum isn''t really a type.
I know... but I don''t want them to simply output to stream as integers, I want them to output as descriptive text.
Yeah... I don''t think enum is a real type.. I was just wondering if there is a way to do it. My initial though was it might be possible... then my second thought was that it wouldn''t be. No terrible loss either way I guess.
You may have to wrap it in a class and use a switch statement.

Wizza Wuzza?
roughly:
typedef enum { A, B, C, D } some_enum;class some_enum_wrapper { some_enum val;explicit some_enum_wrapper(int v):val(v) { }operator<<(blahblahblah) {switch(v){case A:stream << "A";// etc}}}


then just "cout << some_enum_wrapper(enum_value)"

This topic is closed to new replies.

Advertisement