Ordinarily, we “call” an overloaded operator function indirectly by using the operator on arguments of the appropriate type. However, we can also call an overloaded operator function directly in the same way that we call an ordinary function. We name the function and pass an appropriate number of arguments of the appropriate type:
// equivalent calls to a nonmember operator function
data1 + data2; // normal expression
operator+(data1, data2); // equivalent function call
These calls are equivalent: Both call the nonmember function operator+
, passing data1
as the first argument and data2
as the second.
We call a member operator function explicitly in the same way that we call any other member function. We name an object (or pointer) on which to run the function and use the dot (or arrow) operator to fetch the function we wish to call:
data1 += data2; // expression-based ''call''
data1.operator+=(data2); // equivalent call to a member operator function
Each of these statements calls the member function operator+=
, binding this
to the address of data1
and passing data2
as an argument.