C ++中的操作符重载是否比它们值得的麻烦? [英] Are operator overloadings in C++ more trouble than they're worth?

查看:138
本文介绍了C ++中的操作符重载是否比它们值得的麻烦?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我教授C ++的经验中,操作符重载是导致学生最悲痛的主题之一。甚至在stackoverflow看这里的问题:例如,让+操作员外部或成员?如何处理对称性等等,看起来很麻烦。

In my experience teaching C++, operator overloading is one of those topics that causes the most grief to students. Even looking at questions here at stackoverflow: for example, make the + operator external or a member? How to handle symmetry, etc., it seems like it's a lot of trouble.

当我从C ++移植到Java时,我担心我会错过这个能力,但是除了[]或()之类的运算符,我没有真正感到需要超载运算符。事实上,我觉得没有它们的程序更可读。

When I moved from C++ to Java, I was worried I would miss that ability, but with the exception of operators like [] or (), I haven't really ever felt the need to overload operators. In fact, I feel programs without them are more readable.

注意:我把它作为社区wiki。让我们讨论这个。我想听听意见。

Note: I put this as a community wiki. Let's discuss this. I want to hear opinions.

推荐答案

重载的操作符就像spice。有一点可以使事情更好;太多可能使它难吃。

Overloaded operators are like spice. A little can make something better; too much can make it unpalatable.

这篇关于C ++中的操作符重载是否比它们值得的麻烦?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆