大于和小于在一起
Greater than and less than together
我想重载操作符>在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
}
这里是一个演示
要完成这一点,应该为大于操作符添加成员函数重载,并实现它的自由函数重载版本。
相关文章:
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 将图形属性与 std::unique_ptr 捆绑在一起
- 你能在 c++ 中将不同的数字类型加在一起吗?
- 如何将两个字符串加在一起,就好像它们是变量一样?
- 当返回语句时,逗号运算符、大括号初始化列表和 std::unique_ptr 组合在一起
- 为什么push_back和emplace_back结合在一起时,会有不同的行为
- 在浮点精度成为一个问题之前,可以将多少个浮点值加在一起
- 为什么在template函数广播中把两个extensor表达式加在一起不正确
- 实现具有浮点键的类似哈希表的数据结构,其中公差内的值被合并在一起
- 如何在Qt TableView中将列的宽度调整为数据并将最后一部分拉伸在一起
- 编译器如何将链表中的地址字符串在一起?
- 在 c++ 中,两个日志行与 log4Cxx 混合在一起
- 如何将两个 jlong 数据类型转换为 jstring,然后将两个字符串连接在一起以便从 JNI 将字符串返回给 jav
- 将许多向量排序在一起
- C++ std:: 并包括它们如何组合在一起?
- 通过字符串迭代并将每个循环中的先前字符组合在一起
- CMake 项目结构:如何正确地将库合并在一起并将它们包含在多个可执行文件中
- C 操作员超载将3个向量加在一起
- 有没有一种干净(更)的方法将 CRTP 与可变参数继承混合在一起?
- 大于和小于在一起