C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Problem with inheritance

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Victor Chew
Guest





PostPosted: Thu Jul 31, 2003 3:42 am    Post subject: Problem with inheritance Reply with quote



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

Quote:
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:

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


Back to top
Josephine Schafer
Guest





PostPosted: Thu Jul 31, 2003 3:49 am    Post subject: Re: Problem with inheritance Reply with quote




"Victor Chew" <vchew (AT) post1 (DOT) com> wrote

Quote:
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).



Back to top
ES Kim
Guest





PostPosted: Thu Jul 31, 2003 3:50 am    Post subject: Re: Problem with inheritance Reply with quote



"Victor Chew" <vchew (AT) post1 (DOT) com> wrote

Quote:
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



Back to top
Alf P. Steinbach
Guest





PostPosted: Thu Jul 31, 2003 4:12 am    Post subject: Re: Problem with inheritance Reply with quote

On Thu, 31 Jul 2003 11:42:17 +0800, Victor Chew <vchew (AT) post1 (DOT) com> wrote:

Quote:
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;

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


Back to top
Victor Chew
Guest





PostPosted: Thu Jul 31, 2003 5:45 am    Post subject: Re: Problem with inheritance Reply with quote

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:

Quote:
On Thu, 31 Jul 2003 11:42:17 +0800, Victor Chew <vchew (AT) post1 (DOT) com> 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.



Back to top
Kevin Goodsell
Guest





PostPosted: Thu Jul 31, 2003 5:58 am    Post subject: Re: Problem with inheritance Reply with quote

ES Kim wrote:

Quote:


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


Back to top
Victor Chew
Guest





PostPosted: Thu Jul 31, 2003 6:06 am    Post subject: Re: Problem with inheritance Reply with quote

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

Thanks!

Kevin Goodsell wrote:

Quote:
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



Back to top
John Harrison
Guest





PostPosted: Thu Jul 31, 2003 6:15 am    Post subject: Re: Problem with inheritance Reply with quote


"Victor Chew" <vchew (AT) post1 (DOT) com> wrote

Quote:
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



Back to top
Makis Papapanagiotou
Guest





PostPosted: Thu Jul 31, 2003 6:28 am    Post subject: Re: Problem with inheritance Reply with quote

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 (AT) post1 (DOT) com> wrote

Quote:
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 <vchew (AT) post1 (DOT) com> 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.





Back to top
Kevin Goodsell
Guest





PostPosted: Thu Jul 31, 2003 6:41 am    Post subject: Re: Problem with inheritance Reply with quote

Victor Chew wrote:

Quote:
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


Back to top
David White
Guest





PostPosted: Thu Jul 31, 2003 9:44 am    Post subject: Re: Problem with inheritance Reply with quote

"Victor Chew" <vchew (AT) post1 (DOT) com> wrote

Quote:
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




Back to top
Alf P. Steinbach
Guest





PostPosted: Thu Jul 31, 2003 5:13 pm    Post subject: Re: Problem with inheritance Reply with quote

On Thu, 31 Jul 2003 13:45:46 +0800, Victor Chew <vchew (AT) post1 (DOT) com> wrote:

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


Quote:
What is the workaround?

'using', as shown in my first reply.


Back to top
Victor Chew
Guest





PostPosted: Fri Aug 01, 2003 1:40 am    Post subject: Re: Problem with inheritance Reply with quote

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:

Quote:
On Thu, 31 Jul 2003 13:45:46 +0800, Victor Chew <vchew (AT) post1 (DOT) com> 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.



Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.