Ambiguity in Function Overloading
When the compiler could not choose a function between two or more overloaded functions, the situation is called as an ambiguity in function overloading. When the program contains ambiguity, the compiler will not compile the program.
When the compiler could not choose a function between two or more overloaded functions, the situation is called as an ambiguity in function overloading. When the program contains ambiguity, the compiler will not compile the program.
We know that compiler will decide which function to be invoked among the overloaded functions. When the compiler could not choose a function between two or more overloaded functions, the situation is called as an ambiguity in function overloading.
When the program contains ambiguity, the compiler will not compile the program.
The main cause of ambiguity in the program
· Type conversion
· Functions with default arguments
· Functions with a pass by reference
Type conversion
#include <iostream>
using namespace std;
#include <conio.h>
void fun(int i)
{ cout << i;}
void fun(float f)
{ cout << f;}
int main()
{
fun(10);
fun(10.2);
getch();
return 0;
}
In this example type conversion creates ambiguity. fun(10) will refer to the first function.
We think the value 10.2 is floating point value. So fun(10.2) will refer the second function. But fun(10.2) will refer which function? It could not refer to any function. Because 10.2 will be treated as double data type by the compiler. (In c++ all the floating point values are converted by the compiler as a double data type.) This is type conversion of float to double. So now compiler could not search the function fun with a double data type.
So the compiler will give error message call of overloaded ‘fun(double)’ is ambiguous.
This example clearly raises the ambiguity in the program because of the type conversion.
Functions with default arguments
#include <iostream>
using namespace std;
#include <conio.h>
void fun(int i)
{ cout << i;}
void fun(int i, int j=8)
{ cout << i << j;}
int main()
{
fun(10);
fun(10.2f);
getch();
return 0;
}
This program will give error message that overloaded ‘fun(int)’ is ambiguous. Because the fun(int i, int j =8) is a function with default arguments. It can be called in two ways. They are (i) function with one argument.
fun(2) now the value of i is 2 and j is 8 (8 is the default value).
(ii) function with two arguments fun(5,6). So i is 5 and j is 6.
fun(int i) is a function with one argument. It should be invoked with one argument. So
when we call a function with 1 argument the compiler could not select among the two functions fun(int i) and fun(int i, int j=8).
This example clearly illustrates the ambiguity in the program because of function with default arguments.
Function with reference parameter
#include <iostream>
using namespace std;
#include <conio.h>
void fun(int i)
{ cout << i;}
void fun(int &i)
{cout <<i;}
int main()
{
fun(10);
getch();
return 0;
}
This program will show the error message call of overloaded ‘fun(int)’ is ambiguous.
First fun(int) takes one integer argument and second function fun(int &) takes a reference parameter. In this case, compilers do not know which function is intended by the user when it is called. Because there is no syntactical difference in calling the function between the function with single argument and function with single reference parameter.
So compiler could not decide which function should be invoked when it is called. So this leads ambiguity in the program.
Comments
Post a Comment