Reputation:
I have a use case If I have 5 classes A,B,C,D,E and their instances as a,b,c,d,e
If there is an expression a = b + c + d + e;
What is the order of invocation of operator+ and operator=. Also what would be the temporary object created.
Can you please help me
Upvotes: 2
Views: 178
Reputation: 310875
The order of execution is unchanged by the overloading. The normal C++ operator precedence and operand evaluation rules apply.
Upvotes: 0
Reputation: 258568
Full reference here.
The additions are executed first, from right to left. The assignment is the last to get executed. Depending on optimizations, there might not be any temporary produced:
int x,y,z,k;
x = y = z = k = 3;
int l = x + y + z + k;
cout << l;
00401000 mov ecx,dword ptr [__imp_std::cout (402038h)]
00401006 push 0Ch
00401008 call dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (40203Ch)]
Upvotes: 0
Reputation: 153909
There are no sequence points, so the only constraints are that the arguments to an operator must be evaluated before the operator itself is called. In your case:
a = b + c + d + e;
means:
a = (((b + c) + d) + e);
which becomes:
a.operator=( operator+( operator+( operator+( b, c ), d), e ) );
(I'm assuming here that operator=
is a member, as is required by the
standard, but that operator+
is a free function, as is usual.)
As it happens, in this particular expression, operand dependencies impose a strict order.
As for the temporaries, it depends on the implementation of operator+
,
but to give the operator it's usual semantics, it is necessary that each
call return a temporary (and not a reference). In this case, you will
have 3 temporaries, once created at the return from each operator+
,
and all destructed at the end of the full expression.
Upvotes: 0
Reputation: 5744
+
is left associative. =
is right associative. The order would be
d
+ e
makes a temporary de
de
+ c
makes temporary cde
. This is done by a call to class C
's operator +
taking an argument of whatever class the temporary de
is an instance of.cde
+ b
makes temporary bcde
. This is done by a call to class B
's operator +
taking an argument of the class of cde
.bcde
is assigned to variable a
.Temporaries here are a general case, it depends on what you actually do with your overload what will happen. The class to the left of the temporary will be called using that temporary as an argument, not the other way around.
While the evaluation goes from left to right when using +
, the actual computation goes the other way.
Upvotes: 1
Reputation: 1985
Hope it is not a homework
http://en.cppreference.com/w/cpp/language/operator_precedence
+ precedence is higher than =
so + first, and it reads from left to right
then =, and it reads from right to left
For the temporary object, sorry i don't know about it.
Upvotes: 0
Reputation: 206518
a = b + c + d + e;
The expression on the r.h.s of =
will be evaluated first and then =
will be called to assign that result to variable on l.h.s.
The expression on the r.h.s will be evaluated from right to left.
Check operator precedence.
Also, note that generation of temporary in this case will be depend upon:
+
operator &Upvotes: 2