I was wondering if it is possible to have an array value in an enum? Example
enum RGB {
RED[3],
BLUE[3]
} color;
That way RED
could contain values of (255,0,0)
since that is the RGB color code for red.
No you can't do that with an enum. It looks a lot like you want a class/struct:
class Color {
public:
int red;
int green;
int blue;
Color(int r, int g, int b) : red(r), green(g), blue(b) { }
};
Once you have that class defined, you can then put it into a container (eg array, vector) and look them up. You could use an enum to refer to elements in an array, for example.
enum PresetColor {
PRESET_COLOR_RED,
PRESET_COLOR_GREEN,
PRESET_COLOR_BLUE,
};
...
Color presetColors[] = { Color(255, 0, 0), Color(0, 255, 0), Color(0, 0, 255) };
Color favouriteColor = presetColors[PRESET_COLOR_GREEN];
With that in mind, you could wrap all this up to be more maintainable, but I would say that's out of the scope of this question.
You cannot do that. The tokens in an enum
can only hold integral values.
A possible solution:
struct Color {uint8_t r; uint8_t g; uint8_t b;};
Color const RED = {255, 0, 0};
Color const GREEN = {0, 255, 0};
Color const BLUE = {0, 0, 255};
长话短说:不,这是不可能的。
不,枚举的定义只允许它们保存整数值。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.