Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Problem with inheritance

Reply
Thread Tools

Problem with inheritance

 
 
Victor Chew
Guest
Posts: n/a
 
      07-31-2003
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?

 
Reply With Quote
 
 
 
 
Josephine Schafer
Guest
Posts: n/a
 
      07-31-2003

"Victor Chew" <(E-Mail Removed)> wrote in message
news:bga29i$er3$(E-Mail Removed)...
> 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).


 
Reply With Quote
 
 
 
 
ES Kim
Guest
Posts: n/a
 
      07-31-2003
"Victor Chew" <(E-Mail Removed)> wrote in message
news:bga29i$er3$(E-Mail Removed)...
> 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


 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      07-31-2003
On Thu, 31 Jul 2003 11:42:17 +0800, Victor Chew <(E-Mail Removed)> 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.

 
Reply With Quote
 
Victor Chew
Guest
Posts: n/a
 
      07-31-2003
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 <(E-Mail Removed)> 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.
>


 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      07-31-2003
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

 
Reply With Quote
 
Victor Chew
Guest
Posts: n/a
 
      07-31-2003
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
>


 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      07-31-2003

"Victor Chew" <(E-Mail Removed)> wrote in message
news:bgaanf$f9i$(E-Mail Removed)...
> 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


 
Reply With Quote
 
Makis Papapanagiotou
Guest
Posts: n/a
 
      07-31-2003
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" <(E-Mail Removed)> wrote in message
news:bga9h4$f89$(E-Mail Removed)...
> 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 <(E-Mail Removed)> 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.
> >

>



 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      07-31-2003
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ Struct inheritance against class inheritance johnsonlau C++ 1 07-21-2008 04:58 PM
Interface inheritance vs Implementation inheritance. Daniel Pitts Java 27 02-27-2008 01:37 AM
Private Inheritance and Publice Inheritance karthikbalaguru C++ 9 09-10-2007 01:05 PM
mul. inheritance & overloading operator new/delete solved by virtual base inheritance? cppsks C++ 0 10-27-2004 07:49 PM
Private access modifier and Inheritance (Inheritance implementation in Java) maxw_cc Java 1 12-21-2003 11:38 AM



Advertisments