Problem with inheritance

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Victor Chew

    Problem with inheritance

    Can someone tell me why the following code doesn't work:
    [color=blue]
    > TestClass.cpp
    > -------------
    > class A
    > {
    > public:
    > virtual void read(wchar_t& ch) { read(&ch, 0, 1); }
    > virtual void read(wchar_t* buf, int off, int len) = 0;
    > };
    >
    > class B : public virtual A
    > {
    > public:
    > virtual void read(wchar_t* buf, int off, int len) {}
    > };
    >
    > int main(int argc, char* argv[])
    > {
    > wchar_t ch;
    > B myclass;
    > myclass.read(ch );
    > }[/color]

    I have tried both gcc 2.96 and gcc 3.2. I get:
    [color=blue]
    > TestClass.cpp: In function `int main(int, char**)':
    > TestClass.cpp:2 1: no matching function for call to `B::read(wchar_ t&)'
    > TestClass.cpp:1 4: candidates are: virtual void B::read(wchar_t *, int, int)[/color]

    Shouldn't B have inherited read(wchar_t& ch) from A?

  • Josephine Schafer

    #2
    Re: Problem with inheritance


    "Victor Chew" <vchew@post1.co m> wrote in message
    news:bga29i$er3 $1@mawar.singne t.com.sg...[color=blue]
    > Can someone tell me why the following code doesn't work:
    >[color=green]
    > > TestClass.cpp
    > > -------------
    > > class A
    > > {
    > > public:
    > > virtual void read(wchar_t& ch) { read(&ch, 0, 1); }
    > > virtual void read(wchar_t* buf, int off, int len) = 0;
    > > };
    > >
    > > class B : public virtual A
    > > {
    > > public:
    > > virtual void read(wchar_t* buf, int off, int len) {}
    > > };
    > >
    > > int main(int argc, char* argv[])
    > > {
    > > wchar_t ch;
    > > B myclass;
    > > myclass.read(ch );
    > > }[/color]
    >
    > I have tried both gcc 2.96 and gcc 3.2. I get:
    >[color=green]
    > > TestClass.cpp: In function `int main(int, char**)':
    > > TestClass.cpp:2 1: no matching function for call to `B::read(wchar_ t&)'
    > > TestClass.cpp:1 4: candidates are: virtual void B::read(wchar_t *, int,[/color][/color]
    int)[color=blue]
    >
    > Shouldn't B have inherited read(wchar_t& ch) from A?[/color]

    Your base class function virtual void read(wchar_t& ch) has been hidden by
    the derived class
    function virtual void read(wchar_t* buf, int off, int len).


    Comment

    • ES Kim

      #3
      Re: Problem with inheritance

      "Victor Chew" <vchew@post1.co m> wrote in message
      news:bga29i$er3 $1@mawar.singne t.com.sg...[color=blue]
      > Can someone tell me why the following code doesn't work:
      >[color=green]
      > > TestClass.cpp
      > > -------------
      > > class A
      > > {
      > > public:
      > > virtual void read(wchar_t& ch) { read(&ch, 0, 1); }
      > > virtual void read(wchar_t* buf, int off, int len) = 0;
      > > };
      > >
      > > class B : public virtual A
      > > {
      > > public:
      > > virtual void read(wchar_t* buf, int off, int len) {}
      > > };
      > >
      > > int main(int argc, char* argv[])
      > > {
      > > wchar_t ch;
      > > B myclass;
      > > myclass.read(ch );
      > > }[/color]
      >
      > I have tried both gcc 2.96 and gcc 3.2. I get:
      >[color=green]
      > > TestClass.cpp: In function `int main(int, char**)':
      > > TestClass.cpp:2 1: no matching function for call to `B::read(wchar_ t&)'
      > > TestClass.cpp:1 4: candidates are: virtual void B::read(wchar_t *, int, int)[/color]
      >
      > Shouldn't B have inherited read(wchar_t& ch) from A?
      >[/color]

      B::read(wchar_t * buf, int off, int len) hides A::read(wchar_t & ch).
      If you override an overloaded base class functions,
      redefine full set of the functions.

      --
      ES Kim


      Comment

      • Alf P. Steinbach

        #4
        Re: Problem with inheritance

        On Thu, 31 Jul 2003 11:42:17 +0800, Victor Chew <vchew@post1.co m> wrote:
        [color=blue]
        >Can someone tell me why the following code doesn't work:
        >[color=green]
        > > TestClass.cpp
        > > -------------
        >> class A
        >> {
        >> public:
        >> virtual void read(wchar_t& ch) { read(&ch, 0, 1); }
        >> virtual void read(wchar_t* buf, int off, int len) = 0;
        >> };
        >>
        >> class B : public virtual A
        >> {
        >> public:[/color][/color]

        using A::read;
        [color=blue][color=green]
        >> virtual void read(wchar_t* buf, int off, int len) {}
        >> };
        >>
        >> int main(int argc, char* argv[])
        >> {
        >> wchar_t ch;
        >> B myclass;
        >> myclass.read(ch );
        >> }[/color]
        >
        >I have tried both gcc 2.96 and gcc 3.2. I get:
        >[color=green]
        >> TestClass.cpp: In function `int main(int, char**)':
        >> TestClass.cpp:2 1: no matching function for call to `B::read(wchar_ t&)'
        >> TestClass.cpp:1 4: candidates are: virtual void B::read(wchar_t *, int, int)[/color]
        >
        >Shouldn't B have inherited read(wchar_t& ch) from A?[/color]

        It has, but without the 'using' it's hidden by the other read.
        Now don't ask me _why_ someone thought that would be sensible.

        Comment

        • Victor Chew

          #5
          Re: Problem with inheritance

          I don't get it. Isn't overriding based on method signatures? There is
          clearly a difference between read(wchar_t&) and read(wchar_t*, int,
          int)! Why can't I selectively override one of the methods from the base
          class? What is the workaround?

          Alf P. Steinbach wrote:
          [color=blue]
          > On Thu, 31 Jul 2003 11:42:17 +0800, Victor Chew <vchew@post1.co m> wrote:
          >
          >[color=green]
          >>Can someone tell me why the following code doesn't work:
          >>
          >>[color=darkred]
          >>>TestClass.cp p
          >>>-------------
          >>>class A
          >>>{
          >>>public:
          >>> virtual void read(wchar_t& ch) { read(&ch, 0, 1); }
          >>> virtual void read(wchar_t* buf, int off, int len) = 0;
          >>>};
          >>>
          >>>class B : public virtual A
          >>>{
          >>>public:[/color][/color]
          >
          >
          > using A::read;
          >
          >[color=green][color=darkred]
          >>> virtual void read(wchar_t* buf, int off, int len) {}
          >>>};
          >>>
          >>>int main(int argc, char* argv[])
          >>>{
          >>> wchar_t ch;
          >>> B myclass;
          >>> myclass.read(ch );
          >>>}[/color]
          >>
          >>I have tried both gcc 2.96 and gcc 3.2. I get:
          >>
          >>[color=darkred]
          >>>TestClass.cp p: In function `int main(int, char**)':
          >>>TestClass.cp p:21: no matching function for call to `B::read(wchar_ t&)'
          >>>TestClass.cp p:14: candidates are: virtual void B::read(wchar_t *, int, int)[/color]
          >>
          >>Shouldn't B have inherited read(wchar_t& ch) from A?[/color]
          >
          >
          > It has, but without the 'using' it's hidden by the other read.
          > Now don't ask me _why_ someone thought that would be sensible.
          >[/color]

          Comment

          • Kevin Goodsell

            #6
            Re: Problem with inheritance

            ES Kim wrote:
            [color=blue]
            >
            >
            > B::read(wchar_t * buf, int off, int len) hides A::read(wchar_t & ch).
            > If you override an overloaded base class functions,
            > redefine full set of the functions.
            >[/color]

            There is no overriding in this case. Also, redefining the entire set of
            functions would be a pointless waste of time. A using declaration brings
            the base class's overloads into scope nicely.

            -Kevin

            Comment

            • Victor Chew

              #7
              Re: Problem with inheritance

              Do you mind posting a short code segment showing me how to do this?

              Thanks!

              Kevin Goodsell wrote:
              [color=blue]
              > ES Kim wrote:
              >[color=green]
              >>
              >>
              >> B::read(wchar_t * buf, int off, int len) hides A::read(wchar_t & ch).
              >> If you override an overloaded base class functions,
              >> redefine full set of the functions.
              >>[/color]
              >
              > There is no overriding in this case. Also, redefining the entire set of
              > functions would be a pointless waste of time. A using declaration brings
              > the base class's overloads into scope nicely.
              >
              > -Kevin
              >[/color]

              Comment

              • John Harrison

                #8
                Re: Problem with inheritance


                "Victor Chew" <vchew@post1.co m> wrote in message
                news:bgaanf$f9i $1@mawar.singne t.com.sg...[color=blue]
                > Do you mind posting a short code segment showing me how to do this?
                >
                > Thanks!
                >[/color]

                class B : public virtual A
                {
                public:
                virtual void read(wchar_t& ch) { A::read(ch); }
                virtual void read(wchar_t* buf, int off, int len) {}
                };

                john


                Comment

                • Makis Papapanagiotou

                  #9
                  Re: Problem with inheritance

                  Hello,

                  You can use polymorphism in order to have the workaround solution.
                  Even though I wouldn't call it workaround, anyway...

                  A* myclass = new B;
                  myclass.read(ch );

                  Then everything will work properly, since late binding will take part, and
                  it will resolve the correct functions.




                  "Victor Chew" <vchew@post1.co m> wrote in message
                  news:bga9h4$f89 $1@mawar.singne t.com.sg...[color=blue]
                  > I don't get it. Isn't overriding based on method signatures? There is
                  > clearly a difference between read(wchar_t&) and read(wchar_t*, int,
                  > int)! Why can't I selectively override one of the methods from the base
                  > class? What is the workaround?
                  >
                  > Alf P. Steinbach wrote:
                  >[color=green]
                  > > On Thu, 31 Jul 2003 11:42:17 +0800, Victor Chew <vchew@post1.co m> wrote:
                  > >
                  > >[color=darkred]
                  > >>Can someone tell me why the following code doesn't work:
                  > >>
                  > >>
                  > >>>TestClass.cp p
                  > >>>-------------
                  > >>>class A
                  > >>>{
                  > >>>public:
                  > >>> virtual void read(wchar_t& ch) { read(&ch, 0, 1); }
                  > >>> virtual void read(wchar_t* buf, int off, int len) = 0;
                  > >>>};
                  > >>>
                  > >>>class B : public virtual A
                  > >>>{
                  > >>>public:[/color]
                  > >
                  > >
                  > > using A::read;
                  > >
                  > >[color=darkred]
                  > >>> virtual void read(wchar_t* buf, int off, int len) {}
                  > >>>};
                  > >>>
                  > >>>int main(int argc, char* argv[])
                  > >>>{
                  > >>> wchar_t ch;
                  > >>> B myclass;
                  > >>> myclass.read(ch );
                  > >>>}
                  > >>
                  > >>I have tried both gcc 2.96 and gcc 3.2. I get:
                  > >>
                  > >>
                  > >>>TestClass.cp p: In function `int main(int, char**)':
                  > >>>TestClass.cp p:21: no matching function for call to `B::read(wchar_ t&)'
                  > >>>TestClass.cp p:14: candidates are: virtual void B::read(wchar_t *, int,[/color][/color][/color]
                  int)[color=blue][color=green][color=darkred]
                  > >>
                  > >>Shouldn't B have inherited read(wchar_t& ch) from A?[/color]
                  > >
                  > >
                  > > It has, but without the 'using' it's hidden by the other read.
                  > > Now don't ask me _why_ someone thought that would be sensible.
                  > >[/color]
                  >[/color]


                  Comment

                  • Kevin Goodsell

                    #10
                    Re: Problem with inheritance

                    Victor Chew wrote:
                    [color=blue]
                    > Do you mind posting a short code segment showing me how to do this?
                    >
                    > Thanks!
                    >[/color]

                    Please do not top-post. Re-read section 5 of the FAQ for posting
                    guidelines: http://www.parashift.com/c++-faq-lite/how-to-post.html

                    See Alf's reply for the example you requested.

                    -Kevin

                    Comment

                    • David White

                      #11
                      Re: Problem with inheritance

                      "Victor Chew" <vchew@post1.co m> wrote in message
                      news:bga9h4$f89 $1@mawar.singne t.com.sg...[color=blue]
                      > I don't get it. Isn't overriding based on method signatures? There is
                      > clearly a difference between read(wchar_t&) and read(wchar_t*, int,
                      > int)![/color]

                      Please read all replies, such as my other one, if you see it there. It has a
                      link to a thread that explains the rule and why.

                      DW



                      Comment

                      • Alf P. Steinbach

                        #12
                        Re: Problem with inheritance

                        On Thu, 31 Jul 2003 13:45:46 +0800, Victor Chew <vchew@post1.co m> wrote:
                        [color=blue]
                        >I don't get it. Isn't overriding based on method signatures? There is
                        >clearly a difference between read(wchar_t&) and read(wchar_t*, int,
                        >int)! Why can't I selectively override one of the methods from the base
                        >class?[/color]

                        You can, and you just did.

                        It is just as static type B that one 'read' method is hidden. The other
                        is still there. E.g., you can cast it to A& and access the other 'read'.

                        David White here provided a URL to an earlier discussion where Russel
                        Hanneken provided a URL to an even earlier discussion where Chris Newton
                        tried to explain the original thinking, see [http://tinyurl.com/hlts].

                        [color=blue]
                        > What is the workaround?[/color]

                        'using', as shown in my first reply.

                        Comment

                        • Victor Chew

                          #13
                          Re: Problem with inheritance

                          Thanks for all your replies. I support Chris' comment that "I personally
                          regard this decision as unfortunate". If there is a perfect method
                          signature match in the base class which is not overridden in the
                          subclass, then it should simply be used. It's the principle of least
                          surprise.

                          Alf P. Steinbach wrote:
                          [color=blue]
                          > On Thu, 31 Jul 2003 13:45:46 +0800, Victor Chew <vchew@post1.co m> wrote:
                          >
                          >[color=green]
                          >>I don't get it. Isn't overriding based on method signatures? There is
                          >>clearly a difference between read(wchar_t&) and read(wchar_t*, int,
                          >>int)! Why can't I selectively override one of the methods from the base
                          >>class?[/color]
                          >
                          >
                          > You can, and you just did.
                          >
                          > It is just as static type B that one 'read' method is hidden. The other
                          > is still there. E.g., you can cast it to A& and access the other 'read'.
                          >
                          > David White here provided a URL to an earlier discussion where Russel
                          > Hanneken provided a URL to an even earlier discussion where Chris Newton
                          > tried to explain the original thinking, see [http://tinyurl.com/hlts].
                          >
                          >
                          >[color=green]
                          >>What is the workaround?[/color]
                          >
                          >
                          > 'using', as shown in my first reply.
                          >[/color]

                          Comment

                          Working...