运算符重载,运算符+ vs运算符+ = [英] Operator overloading, operator+ vs operator+=

查看:183
本文介绍了运算符重载,运算符+ vs运算符+ =的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读一些c ++源代码,我得到了一些语法。

I was reading through some c++ source code, and i came over some syntax.

path& path::operator+=(string postPath)

我想知道它是否是实际的语法,不是使用已经存在的运算符+,而是将值应用于所讨论的对象。

and i was wondering whether it is actual syntax and why c++ is not using the already existing operator+ instead combined with applying the value to the object in question.

如果你想确保对象被正确删除。但是析构函数应该处理所有这些。

Is it like, if you want to make sure that the object gets deleted properly. But the destructor should deal with all of that.

-Edit1

+ = b;和a + b;

我想知道的是为什么c ++不仅仅使用运算符+ + +,而不必重新定义运算符+ =与运算符相同+

I know there is a difference between a += b; and a + b;
What i was wondering was why does c++ not just use the operator+ with the += instead of having to redefine the operator+= as the same as the operator+

-Edit2

我不确定它是否正确,但我问的是为什么语言不推断+ =基于+。
现在我意识到+ =的其他用法。感谢所有人:)

Im not sure it came across correctly, but what i was asking was why the language doesn't infer += based on +. And now i realize the other uses of +=. Thanks everyone :)

推荐答案


  1. 如果您的对象复制/分配很昂贵,则 a + = b 可能会节省您临时构造,副本构造和分配,并允许您更好地利用您已经有一个目标对象。如果 std :: vector 有一个 + = 运算符来连接向量,则直接实现它会更有效(你可以利用目标对象的额外容量,避免无用的分配),而不是创建一个临时的总向量(从从头开始)并分配它。

  1. There may be efficiency concerns; if your object is expensive to copy/assign, a+=b potentially saves you a temporary construction, a copy construction and an assignment, and allow you to better exploit the resources you already have in a target object. If std::vector had a += operator to concatenate vectors, it would be way more efficient to implement it directly (you can exploit the extra capacity of the target object and avoid useless allocations) instead of creating a temporary sum vector (which starts "from scratch") and assigning it.

实际上,通常我以 + = 的方式实现 + ,通常它产生的代码更高效,写;像这样:

Actually, normally I implement + in terms of +=, usually it yields code that is both more efficient and easier to write; like this:

T &operator+=(const T &r)
{
    // say that T encapsulates an N-dimensional array
    for(int i=0; i<N; ++i)
        arr+=r.arr[i];
    return *this;
}

T operator+(const T &r)
{
    T ret(*this);
    ret+=r;
    return ret;
}

(虽然我同意有办法编译器从现有运算符合成 + / - 对于关系运算符也是如此)

(although I agree that it would be nice to have a way to ask the compiler to synthesize the +/- from the existing operators; the same for the relational operators)

您可能希望有一个可以添加但不能复制/分配的对象。例如,对于我的玩具项目,我简要地考虑了重载 + = 的信号对象(think boost或Qt或.NET事件/多播委托) ( Signal& operator + =(std :: function< T ...> fn)),但我不想处理赋值/复制语义对于信号的IMHO没有意义);

You may want to have an object that can be added something but cannot be copied/assigned. For example, for a toy project of mine I briefly considered having signal objects (think boost or Qt, or .NET events/multicast delegates) that overloaded += to add a connection (Signal &operator+=(std::function<T...> fn)), but I didn't want to deal with the assignment/copy semantic (that IMHO for a signal doesn't make sense);

在底部,一般的想法是在C ++中你可以重载所有的操作符让他们做任何你想要的,没有语义的特殊限制。你可以写一个启动火箭的 operator + 和删除硬盘上的所有文件的 operator + =

At the bottom of it all, there's the general idea that in C++ you can overload (almost) all operators to have them do whatever you want, without particular restrictions of semantics. You can write an operator+ that launches a rocket and an operator+= that deletes all the files on your hard disk - you are in charge, if it makes sense in your application you are free to do it without particular restrictions.

这篇关于运算符重载,运算符+ vs运算符+ =的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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