it first calculates from right to left.... and then performs addition.... so after a++ its still 4(with a promise to increment in next statement)..... then ++a makes it 5..... then further ++a makes it 6.... now the addition phase comes into play.... value of a is 6 now.... so total 18.... if it wud hav been a++ + a++ + a++.... sum wud hav been.... 12.... and for next statement 'a' wud hav been 7....
On Wed, May 30, 2012 at 1:32 AM, Hassan Monfared <[email protected]>wrote: > I implemented ++ for a simple class and got 17. > class A > { > public : > int val; > A(int v):val(v){} > int operator++() > { > cout <<"empty arg called\n"; > return ++val; > } > int operator++(int x) > { > cout <<x<<":arged arg called\n"; > return val++; > } > }; > ------------------ > A b(4); > cout <<++a + ++a +a++<<endl; > ------------ > 17 > but story is different for your sample. > let me tell the fact with a simpler problem : > int b=4; > cout << ++b + ++b ; > will print 12 instead of 11! > amazing huh ? > what happens from right to left is : > in the right statement : b becomes 5: > in the left statement : b becomes 6 ( now be is 6 in both sides !) > so right_b + left_b = 6+6 = 12 > > Regards > > > On Tue, May 29, 2012 at 11:43 PM, Prateek Jain > <[email protected]>wrote: > >> how is it 6? >> ++a(5) + ++a(6) + a++(6) it shud be 17 >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Algorithm Geeks" group. >> To post to this group, send email to [email protected]. >> To unsubscribe from this group, send email to >> [email protected]. >> For more options, visit this group at >> http://groups.google.com/group/algogeeks?hl=en. >> > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
