Imagine we have the type A
and we want to do the following operation:
A operator -(const A& a, const A& b)
{
A c;
//...
return c;
}
When we call this operator, we do something like this:
A c;
c = a - b;
Basically we are allocating c
twice (before the call to the operator and inside of the operator) and then copying its value to the variable on the main code, outside of the operator. In C, we can just pass the pointer to the functions and it will store them exactly where we want them without any intermediate allocation/copy:
void sub(A* a, A* b, A* c);
Is it possible to do the same with operator overloading or do I have to implement this as a C function to avoid that intermediate allocation/copy?
Thanks