Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Operator overload requires parenthesis

I have overloaded both & and *. If I do this:

hgh=(xxx&yy)*vprod1;

It works as expected.

If I do this:

hgh=xxx&yy*vprod1;

I get a compiler error Invalid operands to binary expression.

How does the compiler read this: hgh=xxx&yy*vprod1; -- Wouldn't it move from left to right, just as in the above example, with parenthesis? If the parenthesis were located in a different part of the expression, I can understand how there could be differences in how the compiler read it, but why would that apply here?

It should be worth noting that the return value of both overloads is identical, and returns the same type as xx and yy are in this calculation.

like image 484
johnbakers Avatar asked Jul 13 '26 08:07

johnbakers


1 Answers

* has higher precedence than &, so it's applied first. Your expression is basically xxx & (yy * vprod1);

like image 200
Luchian Grigore Avatar answered Jul 14 '26 22:07

Luchian Grigore



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!