0

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
YAKOVM
  • 9,805
  • 31
  • 116
  • 217

4 Answers4

5

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.

CashCow
  • 30,981
  • 5
  • 61
  • 92
  • Cool, but the problem is that the enums themselves are nameless, so you have to use `int` for variables of any color. – rodrigo Jan 11 '13 at 12:16
  • I have just posted a solution that lets you work around that. – CashCow Jan 11 '13 at 13:35
  • Eventually I need to write a function which returs the color for each of the classes.Can I do it with unnamed enum approach? – YAKOVM Jan 11 '13 at 13:53
  • This now has a ring to java enums, not that it is bad _per se_, but I think it is getting to complex to be used in the real world. – rodrigo Jan 11 '13 at 14:28
0

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.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
0

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 };
};
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • ok someone else has answered about the same but without the information about why it's a bad idea type-wise. i was delayed by Avira anti-virus, which identified [foo.exe] (from running the above code through visual c++ compiler) as malware. shitty AV, to say the least. – Cheers and hth. - Alf Jan 11 '13 at 12:21
0

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
    };
};
Pete Becker
  • 74,985
  • 8
  • 76
  • 165