C
cronusf
I like the C# style enums a lot. In particular, I like how you
specify the enum type followed by the member name, and I like the
ToString conversion. Has anyone done anything like the following in C+
+ for all their enums:
class Day
{
public:
// allow conversion to int
operator int() const;
// allow conversion from int to Day
Day& operator=(int x);
string ToString()const
{
switch(value)
{
case 0: return "Monday";
case 1: return "Tuesday";
...
}
}
static const int Monday = 0;
static const int Tuesday = 1;
static const int Wednesday = 2;
static const int Thursday = 3;
static const int Friday = 4;
static const int Saturday = 5;
static const int Sunday = 6;
private:
int value;
};
Then you can use syntax like:
Day d = Day::Monday;
cout << d.ToString() << endl;
I think any overhead would be negligible. You could even make it a
template so that the underlying type could be any kind of integer
type. It also seems like it would integrate without any issues into
existing code bases. Any problems with this?
specify the enum type followed by the member name, and I like the
ToString conversion. Has anyone done anything like the following in C+
+ for all their enums:
class Day
{
public:
// allow conversion to int
operator int() const;
// allow conversion from int to Day
Day& operator=(int x);
string ToString()const
{
switch(value)
{
case 0: return "Monday";
case 1: return "Tuesday";
...
}
}
static const int Monday = 0;
static const int Tuesday = 1;
static const int Wednesday = 2;
static const int Thursday = 3;
static const int Friday = 4;
static const int Saturday = 5;
static const int Sunday = 6;
private:
int value;
};
Then you can use syntax like:
Day d = Day::Monday;
cout << d.ToString() << endl;
I think any overhead would be negligible. You could even make it a
template so that the underlying type could be any kind of integer
type. It also seems like it would integrate without any issues into
existing code bases. Any problems with this?