|
|
C++'s facility for overloading function names allows complex to handle overloaded function calls in an efficient manner. If a function name is declared to be overloaded, and that name is invoked in a function call, then the declaration list for that function is scanned in order, and the first occurrence of the appropriate function with matching arguments will be invoked. For example, consider the exponential function:
double exp(double); complex exp(complex);When called with a double argument the first, and in this case most efficient, exp() will be invoked. If a complex result is needed, the double result is then implicitly converted using the appropriate constructor. For example:
complex foo = exp(3.5);is evaluated as
complex foo = complex( exp(3.5) );and not
complex foo = exp( complex(3.5) );
Constructors can also be used explicitly. For example:
complex add(complex a1, complex a2) // silly way of doing a1+a2 { return complex( real(a1)+real(a2), imag(a1)+imag(a2) ); }
Inline functions are used to avoid function call overhead for the simplest operations, for example, conj(), +, +=, and the constructors.