C ++中+ =和= +之间的区别

C ++中+ =和= +之间的区别

问题描述:

在用C ++编程时,我经常混淆"+ ="和"= +",前者实际上是我的意思. Visual Studio似乎接受两者,但是它们的行为有所不同,并且是我很多bug的来源.我知道a + = b在语义上等同于a = a + b,但是"= +"的作用是什么?

While programming in C++, I often confuse both "+=" and "=+", the former being the operator I actually mean. Visual Studio seems to accept both, yet they behave differently and is a source for a lot of my bugs. I know that a += b is semantically equivalent to a = a+b, but what does "=+" do?

=+确实是= +(赋值和一元+运算符).

=+ is really = + (assignment and the unary + operators).

为了帮助您记住+=,请记住它先进行加法运算,然后进行赋值运算.当然,这取决于实际的实现,但是应该用于原语.

In order to help you remember +=, remember that it does addition first, then assignment. Of course that depends on the actual implementation, but it should be for the primitives.