Suppose we have this class definition:
class A{
public:
int a;
A(int i): a(i){}
A operator-() const { return A(- a); } # first operator- function
};
A operator-(const A& o) { return A(- o.a); } # second operator- function
Now, in the main function, if I write:
A a1(10);
A a2 = -a1; # Here the first operator- function (method) is called!
However, if I remove the first definition of operator- in the class, the second function is called. I would llike to know why the compiler prefers the first definition when both of them are provided.
I would like to know also why the compiler accept to call the first function (method). Because I think that the method is synonym to a1- and not -a1 : The object on which the method is applied comes before (at the left of) the operator.