Prev: chapter and verse needed for matching new[] to delete[]
Next: YAMI4 - Messaging Solution for Distributed Systems
From: MC on 28 Feb 2010 20:36 I have a class A which overloads + operator; I am confused about the order of evaluation in the following expression A a,b,c,d; d = a + b + c; how is the right hand side of the expression evaluated. Is a+b performed first or b+c performed first? Thanks for any help. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: elazro on 1 Mar 2010 04:26 On Mar 1, 7:36 am, MC <manan.cho...(a)gmail.com> wrote: > I have a class A which overloads + operator; > I am confused about the order of evaluation in the following > expression > > A a,b,c,d; > d = a + b + c; > > how is the right hand side of the expression evaluated. > Is a+b performed first > or b+c performed first? > > Thanks for any help. The + and - operators (as well as *,/,<<, >>, to name a few) have left- to-right associativity, which means a+b+c = (a+b) +c, so a+b would be performed first. Just think of how you would perform a-b-c. Same goes for addition. However, if order matters for your operator overload, you may wish to choose a symbol which does not ordinarily imply full associativity (as '+' does), as that can lead to cognitive dissonance (note your own confusion!). Or, don't use operator overloads at all, if the mismatch is too great. Of course, even the standard library falls short on this front (string contatenation with '+' is not commutative, though it is associative), so there is some leeway (though many folk still think string "addition" should not have been part of the standard library). However, it's pretty easy to abuse operator overloading, so take a moment to decide if it's really worth it in your case. -Matt -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: REH on 2 Mar 2010 02:28 On Mar 1, 4:26 pm, elazro <mah...(a)ncsa.uiuc.edu> wrote: > The + and - operators (as well as *,/,<<, >>, to name a few) have left- > to-right associativity, which means a+b+c = (a+b) +c, so a+b would be > performed first. Just think of how you would perform a-b-c. Same goes > for addition. Actually, you have no guarantee that a+b is performed first. The compiler is perfectly allowed to perform b+c first (or even a+c). Your argument involved subtraction does not apply to addition. Subtraction is anticommutative. Addition is not. REH -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: REH on 2 Mar 2010 02:29
On Mar 1, 4:26 pm, elazro <mah...(a)ncsa.uiuc.edu> wrote: > The + and - operators (as well as *,/,<<, >>, to name a few) have left- > to-right associativity, which means a+b+c = (a+b) +c, so a+b would be > performed first. Just think of how you would perform a-b-c. Same goes > for addition. Oops, sorry. Please ignore my previous post. I missed the part where The OP was asking about *overloaded* operators. For some reason, I assumed he was asking about integers. REH -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |