Let's discuss these two functions:
Where "complex" is the name of the class that implements, for example, a complex variable.
So, the first operator returns a link so that you can write + = b + = c (which is equivalent to b = b + c; a = a + b;).
The second operator returns objec (NOT A REFERENCE), we can still write a = b + c + d.
Who could explain this nuance to me? What is the difference between a returned link or object?
1, a + = b, + = a. , .
2. , a + b , a, a .
:
(a += b) += c;
b, c a. , a += b a. , b + c + d . a .
b
c
a
a += b
b + c + d
, , .
+ , : b + c b, c, - . , b c... , , . , .
, + = , .
complex& operator+=(const T& val); this complex operator+(const T& val); .
complex& operator+=(const T& val);
this
complex operator+(const T& val);
+=, , , , , . , , . (a += b) += c, c , .
+=
(a += b) += c
+, , undefined. a=b+c+d, b+c b1 b1 + d b2, a.
+
a=b+c+d
b+c
b1
b1 + d
b2
In the first case, you add something to the object on the left, but the value of the expression is the object on the left. So you are returning something (usually left) by reference. For instance:
cout << (a+=b)
In the second case, you add two objects and get the third object, and you can do this calculation on the stack so that you return the actual object by value, not by reference. For instance:
if(...) { T a = b + c; cout << a; }
Source: https://habr.com/ru/post/1747526/More articles:WPF DataBinding for standard encoding CLR properties - data-bindingHow to perform an action when changing a remote file (Http)? - httpProfiling PHP Inline - profilingC # - periodic data reading and Thread.Sleep () - multithreadingOut of Process COM Server - function calls and threads - c ++Validation of user data entered - validationUsing LINQ for dynamic mapping (or forecasting) - mappingAnimation by animation - cocoaWebDav timeout - c #What is the correct and reliable way to store an XML string as a JSON property? - jsonAll Articles