overload resolution clarification

Discussion in 'C++' started by sri, Jun 28, 2007.

  1. sri

    sri Guest

    class Base
    {
    public:
    virtual void f(int) { std::cout<<"base.f(int)\n";};
    virtual void f(std::complex<double>) { std::cout<<"derived.f
    \n"; };
    };

    class Derived : public Base
    {
    public :
    virtual void f(double) {std::cout<<"base.f(double)\n";};
    };

    int main()
    {

    Base b; //Line 1
    Derived d; //Line 2
    Base* pb = new Derived; //Line 3
    pb->f(1.0f); //Line 4
    return 0;
    }

    the above program is calling f(int) version, why the overloading
    resolution is calling f(int) version in Base class?

    with Regards,
    Sri
    sri, Jun 28, 2007
    #1
    1. Advertising

  2. sri wrote:
    > class Base
    > {
    > public:
    > virtual void f(int) { std::cout<<"base.f(int)\n";};
    > virtual void f(std::complex<double>) { std::cout<<"derived.f
    > \n"; };
    > };
    >
    > class Derived : public Base
    > {
    > public :
    > virtual void f(double) {std::cout<<"base.f(double)\n";};
    > };
    >
    > int main()
    > {
    >
    > Base b; //Line 1
    > Derived d; //Line 2
    > Base* pb = new Derived; //Line 3
    > pb->f(1.0f); //Line 4
    > return 0;
    > }
    >
    > the above program is calling f(int) version, why the overloading
    > resolution is calling f(int) version in Base class?


    Instead of which one, the 'complex'? User-defined conversion sequence
    (float to std::complex<double>) has lower rank than standard (floating-
    integral) conversion, according to 13.3.3.2/2.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jun 28, 2007
    #2
    1. Advertising

  3. sri

    James Kanze Guest

    On Jun 28, 3:52 pm, sri <> wrote:
    > class Base
    > {
    > public:
    > virtual void f(int) { std::cout<<"base.f(int)\n";};
    > virtual void f(std::complex<double>) { std::cout<<"derived.f
    > \n"; };
    > };


    > class Derived : public Base
    > {
    > public :
    > virtual void f(double) {std::cout<<"base.f(double)\n";};
    > };


    > int main()
    > {
    > Base b; //Line 1
    > Derived d; //Line 2
    > Base* pb = new Derived; //Line 3
    > pb->f(1.0f); //Line 4
    > return 0;
    > }


    > the above program is calling f(int) version, why the overloading
    > resolution is calling f(int) version in Base class?


    And what should it call? Overload resolution is done by the
    compiler, at compile time, and so can only consider the static
    type of the epxression. In this case, Base. There are two f in
    Base, f(int) and f(complex<double>). The conversion double to
    complex<double> formally involves a user defined conversion;
    according to the rules, the compiler chooses a built-in
    conversion over a user defined conversion, even if the built-in
    conversion is lossy, as it is here.

    And of course, since the static type is Base, the compiler
    doesn't see the f(double) in derived at all.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Jun 29, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eph0nk

    Error: Overload resolution failed

    Eph0nk, Oct 23, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,860
    Eph0nk
    Oct 23, 2003
  2. =?Utf-8?B?Q2FpbGlu?=
    Replies:
    0
    Views:
    1,590
    =?Utf-8?B?Q2FpbGlu?=
    Sep 9, 2005
  3. Wolfgang Jeltsch

    overload resolution

    Wolfgang Jeltsch, Aug 31, 2003, in forum: C++
    Replies:
    2
    Views:
    402
    Filipe Sousa
    Aug 31, 2003
  4. Piotre Ugrumov
    Replies:
    3
    Views:
    369
    Nick Hounsome
    Jan 25, 2004
  5. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    248
    Sherm Pendley
    Oct 11, 2004
Loading...

Share This Page