Calling an Overloaded Function

Once we have defined a set of overloaded functions, we need to be able to call them with appropriate arguments. Function matching (also known as overload resolution) is the process by which a particular function call is associated with a specific function from a set of overloaded functions. The compiler determines which function to call by comparing the arguments in the call with the parameters offered by each function in the overload set.

In many—probably most—cases, it is straightforward for a programmer to determine whether a particular call is legal and, if so, which function will be called. Often the functions in the overload set differ in terms of the number of arguments, or the types of the arguments are unrelated. In such cases, it is easy to determine which function is called. Determining which function is called when the overloaded functions have the same number of parameters and those parameters are related by conversions (§ 4.11, p. 159) can be less obvious. We’ll look at how the compiler resolves calls involving conversions in § 6.6 (p. 242).

For now, what’s important to realize is that for any given call to an overloaded function, there are three possible outcomes:

• The compiler finds exactly one function that is a best match for the actual arguments and generates code to call that function.

• There is no function with parameters that match the arguments in the call, in which case the compiler issues an error message that there was no match.

• There is more than one function that matches and none of the matches is clearly best. This case is also an error; it is an ambiguous call.


Exercises Section 6.4

Exercise 6.39: Explain the effect of the second declaration in each one of the following sets of declarations. Indicate which, if any, are illegal.

(a) int calc(int, int);
int calc(const int, const int);

(b) int get();
double get();

(c) int *reset(int *);
double *reset(double *);


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.180.43