why we need type casting for float in function overloading

Discussion in 'C++' started by anand_kr_sin, Nov 30, 2011.

  1. anand_kr_sin

    anand_kr_sin New Member

    Joined:
    Nov 30, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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();
    }
     
    Last edited by a moderator: Nov 30, 2011
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice