Converting enum types
Jul 9, 2009 at 4:31am UTC
Suppose I have:
1 2 3 4 5 6 7 8 9
class MyClass {
public :
enum eNSEW { kN, kS, kE, kW };
static int ToInt( eNSEW nsew ) { ... }
static string ToStr( eNSEW nsew ) { ... }
private :
...
};
Is there a better way to code this up (maybe using C++'s type conversion mechanisms?) so I don't have to call ToInt() or ToStr()? In other words, I would like this call to work:
1 2 3 4 5 6
void foobar( string msg ) { ... }
static MyClass::eNSEW mydir = MyClass::kS;
void myfunc( void ) {
foobar( mydir ); // CRITICAL CALL HERE --------------------
}
without having to call MyClass::ToStr()
Is there any way to do this without "upgrading" the enum to a full-fledged class?
If there is, how?
Last edited on Jul 9, 2009 at 4:50am UTC
Jul 9, 2009 at 4:59am UTC
Is there any way to do this without "upgrading" the enum to a full-fledged class?
No. enums and std::string are incompatible. If you had to convert to an integer it would be a different story, but std::strings are complex types.
Jul 9, 2009 at 7:29am UTC
That's unfortunate that I have to upgrade to a class to get at operator(), which doesn't care if you are converting into a simple or complex type...
Topic archived. No new replies allowed.