运算符重载 c++

发布于 2024-08-21 01:18:40 字数 145 浏览 9 评论 0原文

重载运算符时,是否需要重载>= <=和!=?

对于 C++ 来说,为 !=, !> 调用 !operator= 似乎是明智的做法。对于运算符 <= 和 !<对于运算符>=。

是这样吗,还是有必要重载每个函数?

When overloading operators, is it necessary to overload >= <= and !=?

It seems like it would be smart for c++ to call !operator= for !=, !> for operator<= and !< for operator>=.

Is that the case, or is it necessary to overload every function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

不忘初心 2024-08-28 01:18:40

Boost操作符可能是你在寻找什么。这些将根据一些基本操作符派生出大部分操作符。

C++ 不会自动提供这一点,这是有道理的,因为人们可以为 << 赋予完全不同的含义。例如,和 >(尽管这通常是一个坏主意)。

Boost operators might be what you are looking for. These will derive most of your operators based on a few fundamental ones.

That C++ does not provide this automatically makes sense, as one could give totally different meanings to < and >, for example (although it would often be a bad idea).

Oo萌小芽oO 2024-08-28 01:18:40

在这里我将采取少数派的观点。如果您已经使用 boost,那么使用 boost 运算符并不是什么大问题。这可能是正确且经过测试的做事方式,但仅仅为操作员添加 boost 依赖就有点矫枉过正了。

可以在没有 boost 的情况下编写复杂的 C++ 程序(我个人觉得这在审美上不愉快),因此要保持简单(愚蠢),回答OP的问题,如果你重载operator ==,您还应该重载operator !=。对于 <>++ 等也是如此。

I am going to take a minority viewpoint here. If you already use boost then using boost operators is not that big of a deal. It may be the correct and tested way to do things but adding boost dependency just for the operators is an overkill.

It is possible to write complex C++ programs without boost (which I personally find aesthetically unpleasant) and so to Keep It Simple (Stupid), to answer OP's question, if you overload operator ==, you should also overload operator !=. Same is true for <, >, ++ etc.

妳是的陽光 2024-08-28 01:18:40

是的,如果您希望所有这些都按照您希望的方式工作,那么这是必要的。

C++ 不会对大多数可重载运算符强制任何特定语义。唯一固定的是运算符的一般语法(包括一元或二元以及优先级和结合性等)。这立即意味着您在重载中实现的实际功能可以是绝对任意的。一般情况下,运算符 == 的作用与运算符 != 的作用之间可能没有任何有意义的联系。运算符 == 可能会将数据写入文件,而运算符 != 可能会对数组进行排序。

虽然以如此任意的方式重载运算符肯定不是一个好的编程实践,但 C++ 语言不能假设任何事情。所以,不,它不能也不会自动使用 ! == 组合代替 !=! > 组合代替 <=

Yes, it is necessary, if you want all of them to work the way you want them to work.

C++ does not force any specific semantics on most of the overloadable operators. The only thing that is fixed is the general syntax for the operator (including being unary or binary and things like precedence and associativity). This immediately means that the actual functionality that you implement in your overload can be absolutely arbitrary. In general case there might not be any meaningful connection between what operator == does and what operator != does. Operator == might write data to a file, while operator != might sort an array.

While overloading operators in such an arbitrary fashion is certainly not a good programming practice, the C++ language cannot assume anything. So, no, it cannot and will not automatically use ! == combination in place of !=, or ! > combination in place of <=.

此岸叶落 2024-08-28 01:18:40

不,您只需要重载运算符 == 和运算符 <,标准库将处理其余的:)

(编辑:请参阅使用命名空间 std::rel_ops ;)

No, you only need to overload operator == and operator <, the standard library will take care of the rest :)

(EDIT: see using namespace std::rel_ops ;)

抹茶夏天i‖ 2024-08-28 01:18:40

是的,有必要在定义它们时重载您想要使用的任何运算符 - C++ 不会做出您上面描述的决定;但是,请记住,如果重载的原因是对类进行排序,则只需重写排序例程使用的运算符即可。对于 RTL 排序算法,您只需要覆盖 <和=。

Yes it is necessary to overload whichever operators you want to be used as you define them - C++ will not make the decision you describe above; however, keep in mind that if the reason you are overloading is to sort your class, than you only need to override the operators used by the sort routine. In the case of the RTL sort algorithm you only need to override < and =.

虐人心 2024-08-28 01:18:40

是的!他们在技术上都是不同的运营商。 C++ 编译器本质上不是推理引擎,它们是解析器/编译器。他们只会按照你说的去做。
http://www.parashift.com/c++-faq-lite/ operator-overloading.htmlhttp://en.wikipedia.org/维基/Operators_in_C_and_C%2B%2B

Yes! They are each technically different operators. C++ compilers are not inherently inference engines, they are parsers/compilers. They will only do as much as you say to do.
http://www.parashift.com/c++-faq-lite/operator-overloading.html, http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B

野却迷人 2024-08-28 01:18:40

您可以使用一些快捷方式,例如作为 CRTP 来自动获取它们 注入(参见各种 Compare 类)到您的类中。

There are a few shortcuts you can use, such as CRTP to get them automatically injected (see the various Compare classes) into your class.

你げ笑在眉眼 2024-08-28 01:18:40

作为一种语言,C++ 不会根据任何其他重载运算符来定义任何运算符。仅仅因为您拥有 operator+,并不意味着您可以免费获得 operator+=(与 Ruby 和 Scala 不同)。仅仅因为您有运算符 <== 并不意味着您可以免费获得 <=。仅仅因为您有运算符 == 并不意味着您可以免费获得 != (与 Ruby 和 Scala 不同)。仅仅因为您拥有一元 operator *(一元),并不意味着您可以免费获得 operator ->

std::relops (如果导入正确)并提供默认定义,Boost 提供了一些混合项,根据 <定义所有比较运算符>==。

C++ does not, as a language, define any operator in terms of any other overloaded operator. Just because you have operator+, doesn't mean you get operator+= for free (unlike Ruby and Scala). Just because you have operator < and == doesn't mean you get <= for free. Just because you have operator == doesn't mean you get != for free (unlike Ruby and Scala). Just because you have unary operator * (unary) doesn't mean you get operator -> for free.

std::relops (if imported correctly) and provide default definitions, and Boost provides some mix-ins that define all of the comparison operators in terms of < and ==.

倾城花音 2024-08-28 01:18:40

您可以使用重载来使用用户定义的名称。
运算符重载是指使用同一个运算符对不属于该类别的不同项执行操作。函数重载是指使用相同的函数名称但不同的参数,以克服循环过程中调用同一函数时的开销。

You can use overloading to use user defined names.
Operator overloading means using the same operator to do perform operation on different items which are not in that category. Function overloading means using the same name of function but different arguments, so as to overcome the overhead when the same function is called during looping.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文