I recently saw an example in an operator overloading review where they talked about how the + operator was essentially a function with 2 parameters.
With a bit of poking I decided to look at this a bit deeper and found that calling + like a function does indeed work, just not how you would expect... eg:
int first = 6;
int second = 9;
int result = +(second,first);//result=6
The assembly for this is
int result = +(second,first);
mov eax,dword ptr [first]
mov dword ptr [result],eax
The call to + is simply moving the last parameter into eax.
Can anyone tell me the purpose of this and/or what it is called?
There are two parts to the expression +(second,first)
- and neither of them is a function call.
The expression (second, first)
uses the rare comma operator, which evaluates each expression in turn and the result of the expression is the last expression evaluated.
The +
in this case is just a unary +
operator, like saying +5
or -8
. So the result of your expression is 6
, the value of first
.
You can, however, call the operator +
like this:
int result = operator +(second, first);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With