Function Overloading In C++ (Part II)
A call to an overloaded function is resolved to a particular
instance of the function through a process called argument matching. Argument matching involves
comparing the actual arguments of the call with the formal arguments of each
declared instance of the function.
There are three possible cases, a function call may result in
:
A match : A match is found for the
function call.
No match : No match is found for the
function call.
Ambiguous Match : More than one defined
instance match for a function call.
The compiler tries to find the best possible match for the
function call.
Search for an Exact Match
If the types and number of the actual arguments exactly match
the types and number of formal arguments, the compiler invokes that particular
instance. For example,
Output Screen :
Function 1 called
Reason : The actual argument (here 0)
is int type.
A Match through Promotion
If no exact match is found, an attempt is made to achieve a
match through type promotion of the actual argument.
Integral Promotion : Conversion
of integer types (char, short, enumerator) into int
(if all values of type can be represented by int) or unsigned int
(if all values can’t be represented by int) is called integral promotion.
So, if there is any overloaded function with int formal
type, all smaller integer type (char, short, enumerator)
are converted to int if a perfect match is not found. (Note: In function
overloading, the conversion of smaller integer datatypes to int is
favoured over their conversions to some larger types like long int
or long long int.)
For example,
Output Screen :
Function 1 called
Reason : ‘a’ is of type char and thus promoted to type int after no exact match is found thereby matching f(int).
A match through conversions following Standard Conversion Rules of C++
As C++ puts it, it’s possible to convert any datatype to the
other if we are fine with loss of information in some cases. Following this
rule, the actual argument of any type can be converted to any other type to
match the formal arguments. Here the program may show warnings regarding loss
of information, but it will not be counted as an error. This last step is used
when the matching through the first two steps has failed.
The following program makes the point clearer.
Let us try to understand the steps taken by the compiler one
by one.
Step 1. When the compiler executes the
statement f(‘a’); it first tries to find a
function named f() that has a character type
formal argument. As no such function exists, the compiler takes the next step.
Step 2. As char is a smaller
type than int, compiler tries to find any function named f() with int formal type so that it may
match the function using integral promotion. It fails again and takes the next
step.
Step 3. It then goes for a standard
conversion from char to short (in this case) and thus calls the
function.
But, there is an associated problem with this type conversion.
As any type can be converted to any other type, there is a possibility of
multiple matches. If the compiler encounters such a condition where the actual
argument type can be changed to match the formal types of more than one
function, it is flagged as an error.
Look at the following program.
Output Screen :
[error] call of overloaded
f(char) is ambiguous
[note] candidates are
[note] void f(double)
[note] void f(short int)
The compiler goes to
step 3 as before. But while converting char, it finds two possible matches. It
can convert char to double and also to short int. This results in an error as
shown in the output.
If you find something wrong or if you
wish to add some information in this article, feel free to comment on this post
or contact us at coding.nkcoder@gmail.com.
Which topics you want an article on? Comment down on this
post.
Do not stop here. Read all articles related to C++ here.
If you want to contribute any article for the website, feel
free to share it on coding.nkcoder@gmail.com. We will
publish it on the website with your name.
Post a Comment