大于和小于在一起

Greater than and less than together

本文关键字:在一起 小于 大于      更新时间:2023-10-16

我想重载操作符>在c++中是可能的,写这样的代码:

if(a>x>b)...;

这个操作符只需要两个实参。

你知道怎么做吗?

谢谢!

这是一个相对简单的例子,但它应该做你想做的:

#include <iostream>
struct cool_operator
{
    cool_operator(int _n = 0, bool b = true) : first(b), n(_n) {}
    bool first;
    bool operator <(int x) const
    {
        return first && (n < x);
    }
    int n;
};
cool_operator operator <(int x, cool_operator const &rhs)
{
    return cool_operator(lhs.n, x < rhs.n);
}
int main()
{
    cool_operator c(4);
    std::cout << std::boolalpha << (3 < c < 5); // true
}

这里是一个演示

要完成这一点,应该为大于操作符添加成员函数重载,并实现它的自由函数重载版本。