473,231 Members | 1,435 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

Problem with inheritance

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?

Jul 19 '05 #1
12 3634

"Victor Chew" <vc***@post1.com> wrote in message
news:bg**********@mawar.singnet.com.sg...
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).
Jul 19 '05 #2
"Victor Chew" <vc***@post1.com> wrote in message
news:bg**********@mawar.singnet.com.sg...
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
Jul 19 '05 #3
On Thu, 31 Jul 2003 11:42:17 +0800, Victor Chew <vc***@post1.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.

Jul 19 '05 #4
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 <vc***@post1.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.


Jul 19 '05 #5
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

Jul 19 '05 #6
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


Jul 19 '05 #7

"Victor Chew" <vc***@post1.com> wrote in message
news:bg**********@mawar.singnet.com.sg...
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
Jul 19 '05 #8
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" <vc***@post1.com> wrote in message
news:bg**********@mawar.singnet.com.sg...
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 <vc***@post1.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.

Jul 19 '05 #9
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

Jul 19 '05 #10
"Victor Chew" <vc***@post1.com> wrote in message
news:bg**********@mawar.singnet.com.sg...
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

Jul 19 '05 #11
On Thu, 31 Jul 2003 13:45:46 +0800, Victor Chew <vc***@post1.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.

Jul 19 '05 #12
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 <vc***@post1.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.


Jul 19 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: George Sakkis | last post by:
I'm looking for a design to a problem I came across, which goes like this (no, it's not homework): 1. There is a (single inheritance) hierarchy of domain classes, say A<-B<-..<-Z (arrows point...
4
by: JKop | last post by:
I'm starting to think that whenever you derive one class from another, that you should use virtual inheritance *all* the time, unless you have an explicit reason not to. I'm even thinking that...
3
by: Morten Aune Lyrstad | last post by:
Hi again! I'm having problems with inheritance. I have a base interface class called IObject. Next I have two other interfaces classes, IControl and ICommandMaster, which derives from IObject. ...
0
by: Bita-kookoo | last post by:
I am having a problem with inheritance for my web solution. Here are the steps I took for a project within C#: -I first created a new class, descended from System.Web.UI.Page. For this...
1
by: Galileo | last post by:
I want to ask some questions about ASP, but not ASP. NET, sorry for my cross post because i don't know where asp newsgroup is First, is it possible to use user defined class in ASP, if so, how?...
0
by: Mariano | last post by:
Hi, I have posted a bug of the forms designer that overwrites always the Icon, thus preventing Icon inheritance when you derive from a form. I am trying to overcome this by adding an ImageList in...
9
by: surendran.d | last post by:
hi, can diamond inhertance problem be solved using virtual functions,, or can only be done with scope resolution operators.. is there any other way to solve this problem... Thanks, suri
14
by: dl | last post by:
I have two classes, say A and B, both having a data member 'int n'; private in A, public in B. When I derive class C from both public A and public B, B::n should be visible to C while A::n...
9
by: weird0 | last post by:
How does C++ and C# solve the Diamond problem? With the help of interfaces that is. Can anyone elaborate ....... Regards
3
by: Leo Seccia | last post by:
Hello everyone, I have a c# project with a sql server database. I have a number of lookup tables in my database which I successfully managed to import into my LINQ dataclasses. eg. Table:...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.