2015-06-16 61 views
3

我有一類看起來像這樣:C++運算符重載> = 2不同返回

class Player 
{ 
    friend bool operator>=(Player &pl1, Player &pl2) 
    { 

     return true; 
    }; 
private: 
    int nr, height, weight; 
} 

玩家都有一個編號,身高和體重。 現在我想知道Player1是否比Player2更大和/或更重。在此之後,我想打印出如下:

Player A is 2 cm bigger (smaller) and 7 kg heavier (lighter) than Player B. 

如何才能管理它,當我只能返回true或false?當他變得更大更重或更輕時,我可以迴歸真實,但是我應該如何管理更大/更輕的|更小/更重的情況?

編輯:我必須做到這一點與operator>=。這是我的學校考試,條件是這樣做。這裏是文字:

輸入後,玩家將被顯示。通過使用運算符超載> =將會檢查球員是否比其他球員更大和/或更重。將顯示確定數據的結果,例如:玩家A比玩家B大2釐米(更小)和7公斤重(更輕)。

+0

也許使用自定義枚舉...? –

+1

那麼......你會期待什麼?是一個更小更重的玩家嗎?''''或者'<'更大更輕的玩家?我不認爲運營商超載是在這種情況下最好的方式。 – Marvin

+2

您應該簡單地定義一個具有適當返回類型的額外方法。沒有必要將所有東西都安裝到標準操作符中並使其超載。不要把OOP當作邪教。 – inf

回答

1

所以,如果你組織你的類不同,也許你可以這樣做以下:

class PlayerWeight { 
private: 
    int weight; 
public: 
    int getWeight() const { 
     return weight; 
    } 
    bool operator >=(const PlayerWeight &playerWeight) { 
     return weight >= playerWeight.getWeight(); 
    } 
    PlayerWeight(int weight) : weight(weight) { 
    } 
}; 

class PlayerHeight { 
private: 
    int height; 
public: 
    int getHeight() const { 
     return height; 
    } 
    bool operator >=(const PlayerHeight &playerHeight) { 
     return height >= playerHeight.getHeight(); 
    } 
    PlayerHeight(int height) : height(height) { 
    } 
}; 

class Player : public PlayerHeight, public PlayerWeight { 
public: 
    PlayerHeight& height() { 
     return *this; 
    } 
    PlayerWeight& weight() { 
     return *this; 
    } 
    Player(int height, int weight) : PlayerHeight(height), PlayerWeight(weight) { 
    } 
}; 

int main(int argc, char**argv) { 
    Player playerA(72, 180), playerB(74, 160); 
    // comparison 
    std::cout << "Player A is " << ((PlayerHeight)playerA >= playerB ? "bigger" : "smaller") << " and " << ((PlayerWeight)playerA >= playerB ? "heavier" : "lighter") << std::endl; 
    // or... 
    std::cout << "Player A is " << (playerA.height() >= playerB ? "bigger" : "smaller") << " and " << (playerA.weight() >= playerB ? "heavier" : "lighter") << std::endl; 
    return 0; 
} 
4

如果您的課程不存在嚴格的排序,不要定義排序操作​​符。

相反,你可以寫的功能,如heightDifferenceweightDifference

int heightDifference (const Player& a, const Player& b) 
{ 
    return a.height - b.height; 
} 

然後可以使用這些功能來制定出您所需要的信息。

+0

我也會用功能完成它(它更容易),但這是我必須爲學校做的考試,條件是使用運算符重載> =。 – Rudi

+0

問題描述是否爲操作符重載提供了所需的語義?也許這意味着如果*兩個*屬性都是'> ='另一個'它應該返回'true'。但是,這意味着你的比較將是不對稱的,這是不好的。 – TartanLlama

+0

我在我的1篇文章中添加了考試的內容。我把它從德文翻譯成英文,所以我希望這是可以理解的。 – Rudi

2

您還可以嘗試一種典型的優化問題方法。

當你有多個(衝突)目標時,你可以嘗試對它們的值進行標量聚合/投影。

I.e.你有兩個不同的目標:最大限度地提高體重和身高(>=可以被解釋爲凜然操作!):

double impressive(int height, int weight) 
{ 
    return height + ratio * weight; // some good ratio 

    // lexicographic ordering induced by very large/small values of ratio. 
} 

bool operator>=(Player &a, Player &b) 
{ 
    return impressive(a.height, a.weight) >= impressive(b.height, b.weight); 
}; 

這一切都取決於操作者的語義。

1

你可以做一個比較函數,返回std::pair其中一個給你高度的差異,另一個會給你重量的差異。

using namespace std; 
pair<int,int> compare(Player &a, Player &b) 
{ 
    return make_pair(a.height-b.height, a.weight-b.weight); 
} 

最後,你可以通過簡單地把if-else到任何你想要的比較結果。

0

訂單運算符(<=)的問題在於普通凡人將它們用作完全有序關係:antisymetric,反身性和傳遞性,併爲每對夫婦定義。如果這4個規則中的任何一個不正確請勿使用<=

如果您需要使用您的要求(或至少不違背它)兼容的總訂單,我的建議是:

  • 如果A如果(嚴格)較B大,那麼B < = A(事實上乙< A)
  • 如果A是相同的大小爲B,並且A大於較重的(或相同的重量)乙則B < = A
  • 別的甲< = B(實際上甲< B)

這意味着你將你的兩個訂單關係與一個優先關係結合在一起。