Not virtual good function called

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Remi Ricard

    Not virtual good function called

    Hi,

    This is a code that reproduce my problem.

    #include <stdio.h>
    class A {
    public:
    virtual int get(int a)=0;
    int get(float a) {return 1;}
    };

    class B : public A {
    public:
    virtual int get(int a) { return 2; }
    };

    void main()
    {
    B b;
    printf("b->get(12)=%d b->get(1.234)=%d\ n", b.get(99), b.get(1.234));
    }



    The output is:
    b->get(12)=2 b->get(1.234)=2


    Why the second call b.get(1.2.3.4) don't call the function A::get(float) ??

    I think, I have a conversion from float to int then the function
    B::get(int) is called. Why ??



    Remi Ricard
    ricard@gmc.ulav al.ca

  • Kevin Goodsell

    #2
    Re: Not virtual good function called

    Remi Ricard wrote:
    [color=blue]
    > Hi,
    >
    > This is a code that reproduce my problem.
    >
    > #include <stdio.h>
    > class A {
    > public:
    > virtual int get(int a)=0;
    > int get(float a) {return 1;}
    > };
    > class B : public A {
    > public:
    > virtual int get(int a) { return 2; }[/color]

    This hides any other 'get' function that would have otherwise been
    inherited. You can avoid this with

    using A::get;
    [color=blue]
    > };
    >
    > void main()[/color]

    main returns int in C++. The standard requires this. void has never been
    an acceptable return type for main.
    [color=blue]
    > {
    > B b;
    > printf("b->get(12)=%d b->get(1.234)=%d\ n", b.get(99), b.get(1.234));
    > }
    >
    >
    >
    > The output is:
    > b->get(12)=2 b->get(1.234)=2
    >
    >
    > Why the second call b.get(1.2.3.4) don't call the function A::get(float) ??[/color]

    Because A::get(float) doesn't exist for B. Overload resolution doesn't
    cross inheritance boundaries.
    [color=blue]
    >
    > I think, I have a conversion from float to int then the function
    > B::get(int) is called. Why ??[/color]

    It's the only acceptable overload.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.

    Comment

    • Julián Albo

      #3
      Re: Not virtual good function called

      Remi Ricard escribió:
      [color=blue]
      > #include <stdio.h>
      > class A {
      > public:
      > virtual int get(int a)=0;
      > int get(float a) {return 1;}
      > };
      >
      > class B : public A {
      > public:
      > virtual int get(int a) { return 2; }
      > };
      >
      > void main()
      > {
      > B b;
      > printf("b->get(12)=%d b->get(1.234)=%d\ n", b.get(99), b.get(1.234));
      > }
      >
      > The output is:
      > b->get(12)=2 b->get(1.234)=2
      >
      > Why the second call b.get(1.2.3.4) don't call the function A::get(float) ??[/color]

      The get function in B hides all get functions in A. If you want to make
      it visible you need to put in B:

      using A::get;

      Regards.

      Comment

      • Hafiz Abid Qadeer

        #4
        Re: Not virtual good function called

        Julián Albo <JULIANALBO@ter ra.es> wrote in message news:<3F5F7D8C. E62CA270@terra. es>...[color=blue]
        > Remi Ricard escribi :
        >[color=green]
        > > #include <stdio.h>
        > > class A {
        > > public:
        > > virtual int get(int a)=0;
        > > int get(float a) {return 1;}
        > > };
        > >[/color]
        >[color=green]
        > > class B : public A {
        > > public:
        > > virtual int get(int a) { return 2; }
        > > };
        > >[/color]
        >[color=green]
        > > void main()
        > > {
        > > B b;
        > > printf("b->get(12)=%d b->get(1.234)=%d\ n", b.get(99), b.get(1.2[/color]
        > 34));[color=green]
        > > }
        > >[/color]
        >[color=green]
        > > The output is:
        > > b->get(12)=2 b->get(1.234)=2
        > >[/color]
        >[color=green]
        > > Why the second call b.get(1.2.3.4) don't call the function A::get(float[/color]
        > ) ??
        >
        > The get function in B hides all get functions in A. If you want to make
        > it visible you need to put in B:
        >
        > using A::get;
        >
        > Regards.[/color]

        when you call b.get(1.234)), compiler first looks in B. It finds a
        function that has the same name and 1.234 can be converted to its ats
        argument type (int) so it stops looking further for that name. To call
        the A's function, you have to use the method described in the above
        postings. Hope that helps.

        Regards
        Hafiz Abid Qadeer

        Comment

        • Michiel Salters

          #5
          Re: Not virtual good function called

          hafizabidqadeer @yahoo.com (Hafiz Abid Qadeer) wrote in message news:<9c67da8b. 0309102249.7ccd 2a2d@posting.go ogle.com>...[color=blue]
          > Julián Albo <JULIANALBO@ter ra.es> wrote in message news:<3F5F7D8C. E62CA270@terra. es>...[color=green]
          > > Remi Ricard escribi :
          > >[color=darkred]
          > > > #include <stdio.h>
          > > > class A {
          > > > public:
          > > > virtual int get(int a)=0;
          > > > int get(float a) {return 1;}
          > > > };
          > > >[/color][/color]
          >[color=green][color=darkred]
          > > > class B : public A {
          > > > public:
          > > > virtual int get(int a) { return 2; }
          > > > };[/color][/color][/color]
          [SNIP][color=blue]
          > when you call b.get(1.234)), compiler first looks in B. It finds a
          > function that has the same name and 1.234 can be converted to its ats
          > argument type (int) so it stops looking further for that name.[/color]

          The compiler stops even earlier. Even if you couldn't convert
          1.234 to int, once the compiler has found one get() it will not
          go up one inheritance level. It will search for additional get()
          overloads only in the class where it found the first no matter
          what type of arguments.

          Regards

          Comment

          Working...