Re: Newbie inheritance question
keith@bytebrothers.co.uk 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