C++ 重载两个运算符<;和操作员>;同班

C++ 重载两个运算符<;和操作员>;同班,c++,operator-overloading,C++,Operator Overloading,在我的作业中,我要设计一个班级留言;在其他属性中,它具有属性“priority”(主要目标是实现优先级队列) 和在容器中一样,我必须检查一个对象是否大于另一个对象,我重载了运算符'>'。现在,我有一些关于它的一般性问题 问题一: 如果我重载运算符'>',是否应该重载运算符' 如果我重载运算符'>',是否应该重载运算符'是的,您应该。。。但是,您可以(也可以说应该)在另一个方面实现,=中的三个。这确保了他们的行为一致。通常,(常量消息和lhs、常量消息和rhs) { 返回rhs

在我的作业中,我要设计一个班级留言;在其他属性中,它具有属性“priority”(主要目标是实现优先级队列)

和在容器中一样,我必须检查一个对象是否大于另一个对象,我重载了运算符'>'。现在,我有一些关于它的一般性问题

问题一:

如果我重载运算符'>',是否应该重载运算符'
如果我重载运算符'>',是否应该重载运算符'是的,您应该。。。但是,您可以(也可以说应该)在另一个方面实现
=
中的三个。这确保了他们的行为一致。通常,
(常量消息和lhs、常量消息和rhs)
{
返回rhs
==
=通常是单独实现的。有时类实现
=
,使得
a==b
当且仅当
!(a
,但有时
==
是作为比
更严格的关系实现的!(a
。不过,这样做确实会使类的客户端更加复杂


在某些情况下,可以接受中包含
=
,但不包含
=
= 

如果赋值没有明确要求使用运算符重载,还可以考虑使用函数对象。原因是可能有不止一种方法可以比较两条消息的“小于”(例如按字典顺序比较内容、发布时间等),因此
运算符的含义谢谢,我在某个地方读到,如果我重载一个关系运算,这似乎是合理的超载所有他们。并感谢您的朋友/成员的答案。我明白你的意思:)。这个答案还应该指出,大多数关系运算符可以用少数运算符来描述,通常是
=
可以写成:
bool operator>=(const T&l,const T&r){return!(l
实际上,它们应该这样写。是的,是的,是的!我忘了提这件事,但它很重要。您可能还应该说,干净的代码与手动和独立地编写每个比较代码一样高效。这一点非常重要,因为这意味着为了性能而放弃这一建议的任何尝试都是过早的优化。因此,我们也可以走捷径,比如
类消息:boost::less_than_comparable
,以及
中的
rel_ops
(后者有点可疑,因为它们可以通过使用声明使用)。对于这两个
OperatorToh,尝试使用
rel_ops
可能会非常痛苦。通常,您必须将它们导入另一个名称空间,而且它们往往过于贪婪。如果您的类位于全局名称空间中,您可能最终会使用
rel_ops
为各种不合适的类型提供比较。我认为您最好使用
>rel_ops
在实现文件中用于特殊比较功能,而不是为类永久提供比较运算符。@UncleBens:好的,我误解了。不过我仍然不确定我是否理解你的意思。你的意思是像
{using std::rel_ops::operator>;return lhs>rhs;}
作为操作员的主体>?
if(message1 > message2)
   { ... }
if(message1 < message2)
   { ... }
friend bool operator>(const Message& m1, const Message& m2)
struct RealFraction {
    RealFraction(int x) { this.num = x; this.den = 1; }
    RealFraction(int num, int den) { normalize(num, den); }
    // Rest of code omitted.

    bool operator <(RealFraction const& rhs) {
        return num * rhs.den < den * rhs.num;
    }
};
int x = 1;
RealFraction y = 2;
if (y < x) …
if (x < y) …
inline bool operator>(const Message& lhs, const Message& rhs)
{
    return rhs < lhs;
}

inline bool operator<=(const Message& lhs, const Message& rhs)
{
    return !(rhs < lhs);
}

inline bool operator>=(const Message& lhs, const Message& rhs)
{
    return !(lhs < rhs);
}
#include <queue>
#include <string>
#include <functional>
#include <vector>

class Message
{
    int priority;
    std::string contents;
    //...
public:
    Message(int priority, const std::string msg):
        priority(priority),
        contents(msg)
    {}
    int get_priority() const { return priority; }
    //...
};

struct ComparePriority:
    std::binary_function<Message, Message, bool> //this is just to be nice
{
    bool operator()(const Message& a, const Message& b) const
    {
        return a.get_priority() < b.get_priority();
    }
};

int main()
{
    typedef std::priority_queue<Message, std::vector<Message>, ComparePriority> MessageQueue;
    MessageQueue my_messages;
    my_messages.push(Message(10, "Come at once"));
}
class MessageQueue
{
    std::vector<Message> messages;
    ComparePriority compare;
    //...
    void push(const Message& msg)
    {
        //...
        if (compare(msg, messages[x])) //msg has lower priority
        //...
    }
};