Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Newbie inheritance question

Reply
Thread Tools

Newbie inheritance question

 
 
keith@bytebrothers.co.uk
Guest
Posts: n/a
 
      07-19-2007
Hi all, I'm getting confused again, so someone please take pity and
explain. I've got something like this:

class Base
{
void do_work(string&, string&, int&);
};

class Derived : public Base
{
void do_work(char*, char*, int&);
};

int main()
{
Derived MyObj;
string sString, bString;

MyObj.do_work(aString, bString, 123);
}

This gives a compile-time error that basically says

no matching function for call to `Derived::do_work(string&, string&,
int)'
candidates are: void Derived::do_work(char*, char*, int&)

Why does the compiler not use the matching function from the Base
class? I can work around it by doing something like this:

int main()
{
Base* ptr = new Derived();
string aString, bString;

ptr->do_work(aString, bString, 123);
delete ptr;
}

but that seems to sort of defeat the purpose of inheritance, to my
newbie eyes at least.

 
Reply With Quote
 
 
 
 
Tim Love
Guest
Posts: n/a
 
      07-19-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) writes:

>Hi all, I'm getting confused again, so someone please take pity and
>explain.


> ...


>Why does the compiler not use the matching function from the Base
>class?


It's because of the way the look-up mechanism works. I was getting
surprised by this sort of stuff too, so I put together

http://www-h.eng.cam.ac.uk/help/tpl/...++/lookup.html

which (about half way through) has an example of your situation,
then eventually tries to supply some reasons.


 
Reply With Quote
 
 
 
 
xucaen
Guest
Posts: n/a
 
      07-19-2007
On Jul 19, 5:07 am, (E-Mail Removed) wrote:
> Hi all, I'm getting confused again, so someone please take pity and
> explain. I've got something like this:
>
> class Base
> {
> void do_work(string&, string&, int&);
>
> };
>
> class Derived : public Base
> {
> void do_work(char*, char*, int&);
>
> };
>
> int main()
> {
> Derived MyObj;
> string sString, bString;
>
> MyObj.do_work(aString, bString, 123);
>
> }
>
> This gives a compile-time error that basically says
>
> no matching function for call to `Derived::do_work(string&, string&,
> int)'
> candidates are: void Derived::do_work(char*, char*, int&)
>
> Why does the compiler not use the matching function from the Base
> class? I can work around it by doing something like this:
>
> int main()
> {
> Base* ptr = new Derived();
> string aString, bString;
>
> ptr->do_work(aString, bString, 123);
> delete ptr;
>
> }
>
> but that seems to sort of defeat the purpose of inheritance, to my
> newbie eyes at least.



I guess the short answer is that the Derived::do_work() method hides
the base class method. You will need to either a) add both
declarations in the Base class, or b) change the name of the Derived
class method, or c) add both declarations to the Derived class. My
example below implements options c.
Something else I want to add is that the third parameter is a
reference to an int, so you will need to pass in a variable, not a
constant. I get a compile error when I try passing the constant 123.


class Base
{
public:
virtual void do_work(string&, string&, int&);

};

class Derived : public virtual Base
{
public:
void do_work(string&, string&, int&);
void do_work(char*, char*, int&);

};
void Derived::do_work(string& a, string& b, int& c)
{
cout << a << ", " << b << ", " << c << endl;
}
void int main()
{
Derived MyObj;
string aString, bString;
int myInt = 123;

MyObj.do_work(aString, bString, myInt);

}

Jim

 
Reply With Quote
 
duane
Guest
Posts: n/a
 
      07-19-2007

> I guess the short answer is that the Derived::do_work() method hides
> the base class method. You will need to either a) add both
> declarations in the Base class, or b) change the name of the Derived
> class method, or c) add both declarations to the Derived class. My
> example below implements options c.
> Something else I want to add is that the third parameter is a
> reference to an int, so you will need to pass in a variable, not a
> constant. I get a compile error when I try passing the constant 123.
>
>
> class Base
> {
> public:
> virtual void do_work(string&, string&, int&);
>
> };
>
> class Derived : public virtual Base
> {
> public:
> void do_work(string&, string&, int&);
> void do_work(char*, char*, int&);
>
> };


Wouldn't a using clause work here as well:

class Derived : public virtual Base
{
using Base::do_work;
public:
void do_work(char*,char*,int&);
};

At least this should bring it into scope and allow
an overload?


 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      07-19-2007
(E-Mail Removed) wrote:

> Hi all, I'm getting confused again, so someone please take pity and
> explain. I've got something like this:
>
> class Base
> {
> void do_work(string&, string&, int&);
> };
>
> class Derived : public Base
> {
> void do_work(char*, char*, int&);
> };
>
> int main()
> {
> Derived MyObj;
> string sString, bString;
>
> MyObj.do_work(aString, bString, 123);
> }
>
> This gives a compile-time error that basically says
>
> no matching function for call to `Derived::do_work(string&, string&,
> int)'
> candidates are: void Derived::do_work(char*, char*, int&)


Correct.

> Why does the compiler not use the matching function from the Base
> class?


Uhm, there is no matching function in the base class. Try to compile this:

#include <string>

using std::string;

class Base
{
void do_work(string&, string&, int&);
};

int main()
{
Base MyObj;
string aString, bString;

MyObj.do_work(aString, bString, 123);
}

You will find that the error is still there. The problem is the parameter
123, which will not be used to bind to an int& parameter.


> I can work around it by doing something like this:
>
> int main()
> {
> Base* ptr = new Derived();
> string aString, bString;
>
> ptr->do_work(aString, bString, 123);
> delete ptr;
> }


Apparently, you did not try this code. You would have found that the
compiler still complains. Please post the actual code that gives you
problems.


Best

Kai-Uwe Bux
 
Reply With Quote
 
sumedh.....
Guest
Posts: n/a
 
      07-20-2007
On Jul 19, 2:07 pm, (E-Mail Removed) wrote:
> Hi all, I'm getting confused again, so someone please take pity and
> explain. I've got something like this:
>
> class Base
> {
> void do_work(string&, string&, int&);
>
> };
>
> class Derived : public Base
> {
> void do_work(char*, char*, int&);
>
> };
>
> int main()
> {
> Derived MyObj;
> string sString, bString;
>
> MyObj.do_work(aString, bString, 123);
>
> }
>
> This gives a compile-time error that basically says
>
> no matching function for call to `Derived::do_work(string&, string&,
> int)'
> candidates are: void Derived::do_work(char*, char*, int&)
>
> Why does the compiler not use the matching function from the Base
> class? I can work around it by doing something like this:
>
> int main()
> {
> Base* ptr = new Derived();
> string aString, bString;
>
> ptr->do_work(aString, bString, 123);
> delete ptr;
>
> }
>
> but that seems to sort of defeat the purpose of inheritance, to my
> newbie eyes at least.


well if you want to do anything like this which is function
overriding... u need to have exactly same fucntions in base and
dervied class. Well i think this serves ur purpose....
Cheers,
Sumedh

 
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