Does operator function also cause function overhead

Viewed 98

As per my understanding when we call a non-inlined function like foo() program control will shift to called function address then store the location of caller and return bank to the caller to another statement after previous function class. But when I implement the class with operator definition will the same process occur or something different happens in favor for operator function?

1 Answers

An operator overload is just a function with a peculiar name.
The compiler translates use of the operator into a function call.

That is, a + b becomes a.operator+(b) or operator+(a, b), depending on how the overload is defined.
(You can also write those out yourself, and it will behave exactly the same but miss the point.)

Note that function call overhead is something I haven't seen anyone worry about during this millennium. It only takes nanoseconds on a reasonably modern machine, unless you make very expensive argument copies – but then you get rid of the copying, not the function.

You will very likely never encounter a situation where getting rid of function calls is your top-priority speed optimisation.

Virtual function calls can matter in very time-sensitive situations, for instance in a tight loop, but those instances are rare.
(And the overhead for that is not the function call per se, but is caused by the late binding.)

Related