我正在設計一個撲克遊戲。將有一個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) –