现在我有一个具有5个基本值的枚举,我希望能够将5个基本值中的任何一个相互组合以形成一个组合值,但是我不希望将一个组合值添加到另一个基本值中。
例如:
enum class Color {
Red,
Green,
Blue,
White,
Black
}
Color red = Color::Red;
Color green = Color::Green;
Color yellow = red + green; // Ok; yellow = Color::Yellow (Not sure how I would define this value)
Color orange = yellow + red; // Error; can't add to a combined value
// Note: It would be possible to add the same value to itself
// for example: Color::Red + Color::Red = Color::RedRed
我认为这可能必须更改为带有运算符重载的类,而不是枚举,但是我不确定如何防止将组合值添加到另一个值。
这样做的优雅方法是什么?
最佳答案
我认为不可能完全按照您的要求去做。
我认为您需要两个单独的类Color
和PrimaryColor
。其中PrimaryColor
具有重载的operator+
,允许您组合两个PrimaryColor
并返回Color
。PrimaryColor
可以从Color
派生而来,因此不需要将它们组合在一起的函数可以一视同仁。
您可能想保护构造函数,否则您的限制很容易被规避。
class PrimaryColor;
class Color {
int r, g, b;
explicit Color(int r, int g, int b) : r(r), g(g), b(b) {}
public:
friend bool operator==(const Color& lhs, const Color& rhs) {
return lhs.r == rhs.r && lhs.g == rhs.g && lhs.b == rhs.b;
}
static Color createYellow() { return Color{1, 1, 0}; }
static Color createMagenta() { return Color{1, 0, 1}; }
static Color createCyan() { return Color{0, 1, 1}; }
friend class PrimaryColor;
const static PrimaryColor Red;
const static PrimaryColor Green;
const static PrimaryColor Blue;
const static Color Yellow;
const static Color Magenta;
const static Color Cyan;
};
class PrimaryColor : public Color {
explicit PrimaryColor(int r, int g, int b) : Color(r, g, b) {}
public:
Color operator+(const PrimaryColor& rhs) const {
return Color(r+rhs.r, g+rhs.g, b+rhs.b);
}
static PrimaryColor createRed() { return PrimaryColor{1, 0, 0}; }
static PrimaryColor createGreen() { return PrimaryColor{0, 1, 0}; }
static PrimaryColor createBlue() { return PrimaryColor{0, 0, 1}; }
};
const PrimaryColor Color::Red = PrimaryColor::createRed();
const PrimaryColor Color::Green = PrimaryColor::createGreen();
const PrimaryColor Color::Blue = PrimaryColor::createBlue();
const Color Color::Yellow = Color::createYellow();
const Color Color::Magenta = Color::createMagenta();
const Color Color::Cyan = Color::createCyan();
通过在
PrimaryColor
类中声明静态的Color
和Color
变量,您将获得与您要求的语法接近的内容:void func(const Color& c1, const Color& c2);
int main() {
auto red = Color::Red;
auto green = Color::Green;
auto yellow = red + green; // Ok
assert(yellow == Color::Yellow);
//auto orange = yellow + red; // Error; can't add to a combined value
auto redred = Color::Red + Color::Red;
func(red, yellow); // Pass a PrimaryColor to a function taking a Color
}
Live demo。
编辑:
另外,您可能拥有
PrimaryColor
和HybridColor
,它们都源自一个共同的基类Color
:class PrimaryColor;
class HybridColor;
class Color {
protected:
int r, g, b;
explicit Color(int r, int g, int b) : r(r), g(g), b(b) {}
public:
friend bool operator==(const Color& lhs, const Color& rhs) {
return lhs.r == rhs.r && lhs.g == rhs.g && lhs.b == rhs.b;
}
const static PrimaryColor Red;
const static PrimaryColor Green;
const static PrimaryColor Blue;
const static HybridColor Yellow;
const static HybridColor Magenta;
const static HybridColor Cyan;
};
class HybridColor : public Color {
explicit HybridColor(int r, int g, int b) : Color(r, g, b) {}
public:
friend class PrimaryColor;
static HybridColor createYellow() { return HybridColor{1, 1, 0}; }
static HybridColor createMagenta() { return HybridColor{1, 0, 1}; }
static HybridColor createCyan() { return HybridColor{0, 1, 1}; }
};
class PrimaryColor : public Color {
explicit PrimaryColor(int r, int g, int b) : Color(r, g, b) {}
public:
HybridColor operator+(const PrimaryColor& rhs) const {
return HybridColor(r+rhs.r, g+rhs.g, b+rhs.b);
}
static PrimaryColor createRed() { return PrimaryColor{1, 0, 0}; }
static PrimaryColor createGreen() { return PrimaryColor{0, 1, 0}; }
static PrimaryColor createBlue() { return PrimaryColor{0, 0, 1}; }
};
const PrimaryColor Color::Red = PrimaryColor::createRed();
const PrimaryColor Color::Green = PrimaryColor::createGreen();
const PrimaryColor Color::Blue = PrimaryColor::createBlue();
const HybridColor Color::Yellow = HybridColor::createYellow();
const HybridColor Color::Magenta = HybridColor::createMagenta();
const HybridColor Color::Cyan = HybridColor::createCyan();
Live demo。