Typically, a standard function is called by
functionName(list of arguments).
Another standard way to call a function of an object is
object.functionName(list of arguments).
Both methods are clear and easy to understand since the function signatures are called in the exact order.
However, when it comes to the below:
- A unary operator overloading:
classA::operator-(), for example, doesn't take any argument. So when we writeobject2 = -object1,assumingobject2andobject1are both instances ofclassA, how does C++ know that it has to callclassA::operator-()since we didn't writeobject2 = object1.operator-()? - A global function that defines the + operation between 2 objects for example
Complex operator+(int number, const Complex& c). So when we writeanswer = 10 + aComplexNumber, how does C++ know which function to call since we didn't writeoperator+(10, aComplexNumber)? - A
classA::operator[]operator overloading: so when we callobject[argument]. How does C++ know which function to call since we didn't writeobject.operator[](argument)?
Edit: thank you all for the suggestions. I have edited my question to make it clearer.