Skip to main content This browser is no longer supported. Show
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support. Function Overloading
In this articleC++ lets you specify more than one function of the same name in the same scope. These functions are called overloaded functions, or overloads. Overloaded functions enable you to supply different semantics for a function, depending on the types and number of its arguments. For example, consider a You can overload both member functions and free functions. The following table shows which parts of a function declaration C++ uses to differentiate between groups of functions with the same name in the same scope. Overloading Considerations
ExampleThe following example illustrates how you can use function overloads:
The preceding code shows overloads of the The default argument isn't considered part of the function type. Therefore, it's not used in selecting overloaded functions. Two functions that differ only in their default arguments are considered multiple definitions rather than overloaded functions. Default arguments can't be supplied for overloaded operators. Argument matchingThe compiler selects which overloaded function to invoke based on the best match among the function declarations in the current scope to the arguments supplied in the function call. If a suitable function is found, that function is called. "Suitable" in this context means either:
The compiler creates a set of candidate functions for each argument. Candidate functions are functions in which the actual argument in that position can be converted to the type of the formal argument. A set of "best matching functions" is built for each argument, and the selected function is the intersection of all the sets. If the intersection contains more than one function, the overloading is ambiguous and generates an error. The function that's eventually selected is always a better match than every other function in the group for at least one argument. If there's no clear winner, the function call generates a compiler error. Consider the following declarations (the
functions are marked
Consider the following statement:
The preceding statement builds two sets:
Functions in Set 2 are functions that have implicit conversions from the actual parameter type to the formal parameter type. One of those functions has the smallest "cost" to convert the actual parameter type to its corresponding formal parameter type. The intersection of these two sets is Variant 1. An example of an ambiguous function call is:
The preceding function call builds the following sets:
Because the intersection of these two sets is empty, the compiler generates an error message. For argument matching, a function with n default arguments is treated as n+1 separate functions, each with a different number of arguments. The ellipsis ( Note Ambiguity of overloaded functions can't be determined until a function call is encountered. At that point, the sets are built for each argument in the function call, and you can determine whether an unambiguous overload exists. This means that ambiguities can remain in your code until they're evoked by a particular function call. Argument Type DifferencesOverloaded functions differentiate between argument types that take different initializers.
Therefore, an argument of a given type and a reference to that type are considered the same for the purposes of overloading. They're considered the same because they take the same initializers. For example, For the same reason, function arguments of a type modified by However, the function overloading mechanism can distinguish between references that are qualified by
Output
Pointers to Argument matching and conversionsWhen the compiler tries to match actual arguments against the arguments in function declarations, it can supply standard or user-defined conversions to obtain the correct type if no exact match can be found. The application of conversions is subject to these rules:
The resultant sequence of conversions, if any, is called the best matching sequence. There are several ways to convert an object of type
Although the first sequence achieves the desired goal, it isn't the best matching sequence, because a shorter sequence exists. The following table shows a group of conversions called trivial conversions. Trivial conversions have a limited effect on which sequence the compiler chooses as the best match. The effect of trivial conversions is described after the table. Trivial conversions
The sequence in which conversions are attempted is as follows:
Graph showing preferred conversions. Conversion from type This same rule applies
to reference conversions. Conversion from type This same rule applies to pointer-to-member conversions. Conversion from type The preceding rule applies only along a given path of derivation. Consider the graph shown in the following figure. Multiple-inheritance graph that shows preferred conversions. Conversion from type
User-defined conversions are applied if no built-in promotion or conversion exists. These conversions are selected based on the type of the argument being matched. Consider the following code:
The available user-defined conversions for class During the process of matching arguments, standard conversions can be applied to both the argument and the result of a user-defined conversion. Therefore, the following code works:
In this example, the compiler invokes a user-defined conversion, If any user-defined conversions are required to match an argument, the standard conversions aren't used when evaluating the best match. Even if more than one candidate function requires a user-defined conversion, the functions are considered equal. For example:
Both versions of
Even though the second one requires both a standard conversion and the user-defined conversion, the two conversions are still considered equal. Note User-defined conversions are considered conversion by construction or conversion by initialization. The compiler considers both methods equal when it determines the best match. Argument matching and the this pointerClass member functions are treated differently, depending on whether they're declared as Member functions that aren't Unlike other arguments in overloaded functions, the compiler introduces no temporary objects and attempts no conversions when trying to match the When the The
The left operand of the Reference-qualifiers on member functionsReference qualifiers make it possible to overload a member function based on whether the object pointed to by
Restrictions on overloadingSeveral restrictions govern an acceptable set of overloaded functions:
Overloading, overriding, and hidingAny two function declarations of the same name in the same scope can refer to the same function, or to two discrete overloaded functions. If the argument lists of the declarations contain arguments of equivalent types (as described in the previous section), the function declarations refer to the same function. Otherwise, they refer to two different functions that are selected using overloading. Class scope is strictly observed. A function declared in a base
class isn't in the same scope as a function declared in a derived class. If a function in a derived class is declared with the same name as a If the base class function isn't declared as Block scope is strictly observed. A function declared in file scope isn't in the same scope as a function declared locally. If a locally declared function has the same name as a function declared in file scope, the locally declared function hides the file-scoped function instead of causing overloading. For example:
The preceding code shows two definitions from the
function For overloaded member functions, different versions of the function can be given different access privileges. They're still considered to be in the scope of the enclosing class and thus are overloaded
functions. Consider the following code, in which the member function The intent of this sample is to provide an The call to
See alsoFunctions (C++) FeedbackSubmit and view feedback for Where should default parameters A in a function prototype?Explanation: Default parameters are defined to the rightmost side of parameter list in a function to differentiate between the normal and default parameters for example if a function is defined as fun(int x = 5, int y) then if we call fun(10) then 10 should be given to x or y because one can apply both logics like x = ...
What is function prototype when is it optional to have a prototype?a function prototype merely specifies its(the function) interface(I/O). Prototype of a function is also called signature of the function. If a call or reference has to be made to a function in a code section before the actual function definition, then the function prototype is compulsory.
What is not included in function prototype?In computer programming, a function prototype or function interface is a declaration of a function that specifies the function's name and type signature (arity, data types of parameters, and return type), but omits the function body.
Is it possible for a function to have parameters with default arguments and some without?It is possible for a function to have some parameters with default arguments and some without. A function's return data type must be the same as the function's parameter(s). One reason for using functions is to break programs into manageable units, or modules. You must furnish an argument with a function call.
|