Re: How to define operator "const char *" of enumerated type defined inside a class
I want to be able to write (const char*)v where v is an item of type
Class::ToolTypeT where ToolTypeT is an enumeration and I've tried
everything that looks sensible. There's an ugly solution, but surely this
is
possible?
"Sam" <sam@email-scan.com> wrote in message
news:cone.1222254608.28545.7466.500@commodore.email-scan.com...
You can't really do this with merely an enum. You can, however, define a
class that, essentially, acts like an enum.
class Class {
public:
static const int a=0;
static const int b=1;
static const int c=2;
class ToolTypeT {
int v;
public:
ToolTypeT(int i): v(i) {}
ToolType &operator=(int i) { v=i; return *this; }
operator int() const { return v; }
operator const char *();
};
};
You can use this Class::ToolTypeT pretty much like an enumeration. Class::a,
Class::b, Class::c, etc. are your enumerator values. And, you can supply a
meaningful operator const char *().
And, with a few more tweaks, you can add additional type-safety to the whole
schema.
Bill Davy writes:
That's an interesting approach. Can switch on ToolTypeT which is good. Can
even make a,b,c into an eunumeration (which may help some tools notice if I
have missed one out). And the type conversion is there.
Many thanks,
Bill