Problem with inheritance

Discussion in 'C++' started by Victor Chew, Jul 31, 2003.

  1. Victor Chew

    Victor Chew Guest

    Can someone tell me why the following code doesn't work:

    > 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);
    > }


    I have tried both gcc 2.96 and gcc 3.2. I get:

    > TestClass.cpp: In function `int main(int, char**)':
    > TestClass.cpp:21: no matching function for call to `B::read(wchar_t&)'
    > TestClass.cpp:14: candidates are: virtual void B::read(wchar_t*, int, int)


    Shouldn't B have inherited read(wchar_t& ch) from A?
    Victor Chew, Jul 31, 2003
    #1
    1. Advertising

  2. "Victor Chew" <> wrote in message
    news:bga29i$er3$...
    > Can someone tell me why the following code doesn't work:
    >
    > > 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);
    > > }

    >
    > I have tried both gcc 2.96 and gcc 3.2. I get:
    >
    > > TestClass.cpp: In function `int main(int, char**)':
    > > TestClass.cpp:21: no matching function for call to `B::read(wchar_t&)'
    > > TestClass.cpp:14: candidates are: virtual void B::read(wchar_t*, int,

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


    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).
    Josephine Schafer, Jul 31, 2003
    #2
    1. Advertising

  3. Victor Chew

    ES Kim Guest

    "Victor Chew" <> wrote in message
    news:bga29i$er3$...
    > Can someone tell me why the following code doesn't work:
    >
    > > 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);
    > > }

    >
    > I have tried both gcc 2.96 and gcc 3.2. I get:
    >
    > > TestClass.cpp: In function `int main(int, char**)':
    > > TestClass.cpp:21: no matching function for call to `B::read(wchar_t&)'
    > > TestClass.cpp:14: candidates are: virtual void B::read(wchar_t*, int, int)

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


    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
    ES Kim, Jul 31, 2003
    #3
  4. On Thu, 31 Jul 2003 11:42:17 +0800, Victor Chew <> wrote:

    >Can someone tell me why the following code doesn't work:
    >
    > > 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:


    using A::read;

    >> 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.cpp: In function `int main(int, char**)':
    >> TestClass.cpp:21: no matching function for call to `B::read(wchar_t&)'
    >> TestClass.cpp:14: candidates are: virtual void B::read(wchar_t*, int, int)

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


    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.
    Alf P. Steinbach, Jul 31, 2003
    #4
  5. Victor Chew

    Victor Chew Guest

    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:

    > On Thu, 31 Jul 2003 11:42:17 +0800, Victor Chew <> wrote:
    >
    >
    >>Can someone tell me why the following code doesn't work:
    >>
    >>
    >>>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:

    >
    >
    > using A::read;
    >
    >
    >>> 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.cpp: In function `int main(int, char**)':
    >>>TestClass.cpp:21: no matching function for call to `B::read(wchar_t&)'
    >>>TestClass.cpp:14: candidates are: virtual void B::read(wchar_t*, int, int)

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

    >
    >
    > 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.
    >
    Victor Chew, Jul 31, 2003
    #5
  6. ES Kim wrote:

    >
    >
    > 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.
    >


    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
    Kevin Goodsell, Jul 31, 2003
    #6
  7. Victor Chew

    Victor Chew Guest

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

    Thanks!

    Kevin Goodsell wrote:

    > ES Kim wrote:
    >
    >>
    >>
    >> 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.
    >>

    >
    > 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
    >
    Victor Chew, Jul 31, 2003
    #7
  8. "Victor Chew" <> wrote in message
    news:bgaanf$f9i$...
    > Do you mind posting a short code segment showing me how to do this?
    >
    > Thanks!
    >


    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
    John Harrison, Jul 31, 2003
    #8
  9. 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" <> wrote in message
    news:bga9h4$f89$...
    > 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:
    >
    > > On Thu, 31 Jul 2003 11:42:17 +0800, Victor Chew <> wrote:
    > >
    > >
    > >>Can someone tell me why the following code doesn't work:
    > >>
    > >>
    > >>>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:

    > >
    > >
    > > using A::read;
    > >
    > >
    > >>> 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.cpp: In function `int main(int, char**)':
    > >>>TestClass.cpp:21: no matching function for call to `B::read(wchar_t&)'
    > >>>TestClass.cpp:14: candidates are: virtual void B::read(wchar_t*, int,

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

    > >
    > >
    > > 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.
    > >

    >
    Makis Papapanagiotou, Jul 31, 2003
    #9
  10. Victor Chew wrote:

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


    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
    Kevin Goodsell, Jul 31, 2003
    #10
  11. Victor Chew

    David White Guest

    "Victor Chew" <> wrote in message
    news:bga9h4$f89$...
    > 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)!


    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
    David White, Jul 31, 2003
    #11
  12. On Thu, 31 Jul 2003 13:45:46 +0800, Victor Chew <> wrote:

    >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?


    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].


    > What is the workaround?


    'using', as shown in my first reply.
    Alf P. Steinbach, Jul 31, 2003
    #12
  13. Victor Chew

    Victor Chew Guest

    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:

    > On Thu, 31 Jul 2003 13:45:46 +0800, Victor Chew <> wrote:
    >
    >
    >>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?

    >
    >
    > 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].
    >
    >
    >
    >>What is the workaround?

    >
    >
    > 'using', as shown in my first reply.
    >
    Victor Chew, Aug 1, 2003
    #13
    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. maxw_cc
    Replies:
    1
    Views:
    3,137
    Martijn van Steenbergen
    Dec 21, 2003
  2. cppsks
    Replies:
    0
    Views:
    819
    cppsks
    Oct 27, 2004
  3. karthikbalaguru
    Replies:
    9
    Views:
    1,036
  4. Daniel Pitts
    Replies:
    27
    Views:
    1,898
    Mike Schilling
    Feb 27, 2008
  5. johnsonlau
    Replies:
    1
    Views:
    772
    Kai-Uwe Bux
    Jul 21, 2008
Loading...

Share This Page