问题
I have several classes each of them uses the same enum
but extends it a bit, based on its requirements.
For example :
class skirtColor{
enum Color{
red = 1,
blue = 10,
green = 12
};
};
class dressColor {
enum Color{
red = 1,
pink,
yellow,
blue = 10,
green = 12
};
};
class pantsColor {
enum Color {
red = 1,
brown,
blue = 10,
green = 12
};
};
Since there is no inheritance for enum in C++, I would like to use define
for a common part
#define COLOR\
// red color \
red = 1,\
// blue color \
blue = 10,\
//green color
green = 12,
After that I can reuse common color definition in classes
class skirtColor{
enum Color{
COLOR
};
};
class dressColor {
enum Color{
COLOR
pink = 2,
yellow,
};
};
Class pantsColor {
enum Color {
COLOR
brown = 2,
};
};
Is this way OK? I can't compile this code can you help me with correct macro definition?
回答1:
One way you can manage this kind of thing at present is inheritence which will inherit the constants from the class.
struct Color
{
enum { red = 1, blue = 10, green = 12 };
} ;
struct DressColor : Color
{
enum { pink =2, yellow = 3 };
};
and DressColor will also have red, blue and green..
If you want to enable "strict typing" to have a variable that must have one of these values you can do this with a class that "has-a" value and can only be constructed or modified from within the class.
struct Color
{
class Holder
{
private:
int value;
explicit Holder( int v ) : value( v ) {}
public:
bool operator==( const Holder & other ) const
{
return value == other.value;
}
int get() const
{
return value;
}
friend struct Color;
};
protected:
static Holder makeHolder( int v )
{
return Holder( v );
}
public:
static const Holder red;
static const Holder blue;
static const Holder green;
};
struct DressColor : Color
{
static const Holder pink;
static const Holder yellow;
};
// these in a .cpp file.
const Color::Holder Color::red( 1 );
const Color::Holder Color::blue( 10 );
const Color::Holder Color::green( 12 );
const Color::Holder DressColor::pink( Color::makeHolder(2) );
const Color::Holder DressColor::yellow( Color::makeHolder(3) );
// how you can create a variable of one. Note that whilst you cannot construct
// a Color::Holder from an int in the regular way, you can copy-construct.
Color::Holder var( Color::red );
Of course in this "workaround", the enum objects are still of type Color::Holder
and cannot be of type DressColor::Holder
for using class DressColor
.
回答2:
http://ideone.com/Q2mIRk
Color
is not the same as COLOR
here:
Enum Color{
Color
Pink = 2,
yellow,
};
Also, it's enum
and class
, all lower-case.
Also, ditch the comments:
#define COLOR\
// red color \
red = 1,\
// blue color \
blue = 10,\
//green color
green = 12,
because the trailing \
will make everything into a comment. The above will expand to:
// red color red = 1, // blue color blue = 10, //green color green = 12,
Just say
#define COLOR \
red = 1,\
blue = 10,\
green = 12,
if there's someone that can't figure out what color red
stands for... well, I'll just keep my mouth shut.
回答3:
Your code does not even get the C++ keywords correct.
And instances of your …Color
classes do not represent colors, so the class names are somewhat misleading.
But anyway…
Extending an enum
is non-trivial in that it requires superclassing, not subclassing, which is why it isn't supported by the language.
But it seems you're only interested in having the names available, not any strong typing, in which case you can do …
struct SkirtColors
{
enum { red = 1, blue = 10, green = 12 };
};
struct DressColors
: SkirtColors
{
enum { pink = 2, yellow = 3 };
};
回答4:
I'd be inclined to start with an enum
that names all of the colors, then add enums for the various specific ones and use the values from the first:
struct colors {
enum colors {
red = 1,
blue = 10,
pink = 2,
green = 12,
yellow = 3
};
};
struct skirt_colors {
enum colors {
red = colors::red,
blue = colors::blue,
green = colors::green
};
};
来源:https://stackoverflow.com/questions/14277671/define-inside-an-enum-or-how-to-extend-an-enum