Re: enum question
On Apr 29, 1:10 pm, www <w...@nospam.com> wrote:
I have three enums:
enum MyEnum
{
A, B, C, D
}
enum HisEnum
{
A, B, C, D, M, N
}
enum HerEnum
{
A, B, C, D, P
}
A, B, C, and D shows up three times in three enums. I am wondering if
the code can be improved to avoid such repeated coding. Ideally, I would
think MyEnum can be the "parent class" of HisEnum and HerEnum. But enum
cannot sublclass.
Consider the following:
enum GolfClub
{
DRIVER,
IRON,
WEDGE,
PUTTER;
}
enum OSComponent
{
DRIVER,
DAEMON,
USER_PROGRAM;
}
enum ShippingEmployee
{
DRIVER,
PACKER,
MANAGER,
CLERK;
}
All these enums have a constant "DRIVER". In what way would it make
sense to have the "DRIVER" constant be common to all three enums?
It wouldn't!
Just because something has the same name doesn't mean it's the same
thing. The whole freaking point of enums is to distinguish same-named
things by type so that the names are distinct. Now you want to go and
destroy that advantage?
Don't forget that enums are classes, and the constants have
behaviors. Good luck defining the 'getYardageRange()' of an
OSComponent.DRIVER or the 'getTimingClock()' of a GolfClub.DRIVER.
Either make an enum that has all the constants it needs, or write your
own type-safe enumeration class without using 'enum' so that they can
be heritable.
--
Lew