我正在设计一个扑克游戏。将有一个PokerHand类用户卡(总是5张卡)。 PokerHand有不同的类别,例如直线,冲洗,满屋等比较没有RTTI的抽象类?
现在我想要所有的PokerHand都是可比的。有一个定义的类别之间的顺序:直冲>一种>满屋> ...对于每个类别都有一个独特的比较规则。例如。对于Straights,高牌决定。对于“4种”,4张相同的牌决定。
class PokerHand {
public:
int CompareTo(const PokerHand* another) = 0;
Category GetCagegory();
...
}
有了RTTI,我可以实现的CompareTo作为
class Straight : public PokerHand {
...
}
int Straight::CompareTo(const PokerHand& another) OVERRIDE {
const Straight* s = dynamic_cast<const Straight*>(another);
if (s == NULL) {
// Not a straight. Compare category.
...
} else {
// compare high card
...
}
}
现在的问题是,考虑RTTI大多以“不建议使用”治疗,有没有落实不比较的好方法使用RTTI?
你可以有一个包含类的虚拟功能,但我想你会了解它的错误的方式......我会写一个答案到那种程度。 –
为各种不同的扑克牌手分别创建一个类似乎是一个糟糕的主意。有很多这样的东西,它们重叠。防爆。皇家同花顺,同花顺,只是简单的冲洗。 – seand
[什么时候应该使用访问者设计模式?](http://stackoverflow.com/questions/255214/when-should-i-use-the-visitor-design-pattern) –