Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   why we need type casting for float in function overloading (http://www.go4expert.com/forums/type-casting-float-function-overloading-t27253/)

anand_kr_sin 30Nov2011 13:32

why we need type casting for float in function overloading
 
The function was like
Code:

class calc
{
float area(float a)
{
    return a*a;
}
int area(int a)
{
return a*a;
}
};
main()
{
calc ob1;
ob1.area(10);
ob1.area(10.5);
getch();
}


xpi0t0s 1Dec2011 18:24

Re: why we need type casting for float in function overloading
 
Because
Code:

int foo(int bar)
{
...
}

int quux()
{
  foo(10.5);
}

is legal, so the compiler doesn't know if the second call to area() is to area(int(10.5)) or area(float). The cast removes the ambiguity.


All times are GMT +5.5. The time now is 09:33.