Newbie inheritance question

K

keith

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.
 
T

Tim Love

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/languages/C++/lookup.html

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

xucaen

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
 
D

duane

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?
 
K

Kai-Uwe Bux

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
 
S

sumedh.....

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top