• Popular Now

• 10
• 9
• 12
• 14
• 15

Archived

This topic is now archived and is closed to further replies.

Anonymous Enumeration to Stream

This topic is 5315 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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

Share on other sites
C++ will convert enumerations to integers; you don''t need a custom << operator.

Share on other sites
afaik, enum isn''t really a type.

Share on other sites
I know... but I don''t want them to simply output to stream as integers, I want them to output as descriptive text.

Share on other sites
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.

Share on other sites
You may have to wrap it in a class and use a switch statement.

Wizza Wuzza?

Share on other sites
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)"