C++ 运算符重载 >= 有 2 个不同的 returns

C++ operator overloading >= with 2 different returns

我有一个 class 看起来像这样:

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

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

一名球员有号码、身高和体重。 现在我想知道 Player1 是否比 Player2 大 and/or 重。之后我想像这样打印出来:

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

只能return真假怎么办?当他又大又重时我可以 return true 或他小而轻时 false,但是我应该如何管理 bigger/lighter | smaller/heavier 案例?

编辑:我必须用 operator>= 来完成。这是我学校的考试,条件是这样。正文如下:

After the input the players will be displayed. By using operator overloading >= will be checked if the player is bigger and/or heavier than the other player. The result with the determined data will be displayed, e.g.: Player A is 2 cm bigger (smaller) and 7 kg heavier (lighter) than Player B.

如果您的 类 不存在严格的排序,请不要定义排序运算符。

相反,您可以编写 heightDifferenceweightDifference:

这样的函数
int heightDifference (const Player& a, const Player& b)
{
    return a.height - b.height;
}

然后您可以使用这些函数计算出您需要的信息。

您也可以尝试优化问题的典型方法。

当您有多个(相互冲突的)目标时,您可以尝试获取它们值的标量 aggregation/projection。

即你有两个不同的目标:最大化体重和身高(>= 可以解释为 令人敬畏的 运算符!):

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);
};

这完全取决于运算符的语义。

你可以做一个比较函数 return 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 与您想要的任何内容进行比较来比较您的结果。

顺序运算符 (<=) 的问题是普通人将它们用作完整的顺序关系:反对称、自反和传递,并为每一对定义。如果这 4 条规则中的任何一条不成立 请勿使用 <=.

如果您需要一个符合您要求的总订单(或至少不违反它),我的建议是:

  • 如果 A 如果(严格)大于 B 那么 B <= A(实际上 B < A)
  • 如果 A 与 B 大小相同,并且 A 比 B 重(或重量相同)则 B <= A
  • 否则 A <= B(实际上 A < B)

这意味着您将两个顺序关系与优先顺序组合在一起。

因此,如果您以不同的方式组织 class,也许您可​​以执行以下操作:

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;
}