Cannot access overloaded function which has no arguments

Take a look at the attached code.  When I compile this (using Visual C++), I get an error on the p->load() line which says there is no 'load' function which takes zero arguments.  Why can I not inherit this function from Class1?  Note that in the case of 'loadx', which does not exist in Class2, I can call the inherited function.  Also, if I cast the Class2 pointer to Class1, I can call 'load'.

Why does C++ behave like this?
class Class1
{
public:
	int load(void)
	{
		return(5);
	}
 
	int loadx(void)
	{
		return(8);
	}
};
 
class Class2 : public Class1
{
public:
	int load(int arg)
	{
		return(7);
	}
};
 
 
int _tmain(int argc, _TCHAR* argv[])
{
	Class2 * p = new Class2();
 
	p->load();
	p->loadx();
 
	static_cast<Class1 *>(p)->load();
 
	return 0;
}

Open in new window

LVL 9
JohnGabyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pgnatyukCommented:
You overloaded the function from the basic class. The code looks correct.
If we will open the book, we will fine something wise like: "if B is a subclass of A, then it must be valid to use a B wherever you could use an A."
 Also it looks fine and will work:
Class2 two;
((Class1)two).load();
0
JohnGabyAuthor Commented:
I realize that if you use a cast, you can call the load() (I mentioned that in my first post).  What I don't understand is WHY I need to have the cast at all.  There is no definition for a load function with no parameters in Class2, so why can I not call the load directly.  Note that if there is no overload of the function (as is the case for the loadx function), then I can call the function from Class1 without the cast.

I really don't like using casts, unless it is absolutely necessary, and I don't understand why it is necessary in this case.
0
pgnatyukCommented:
One more thing:
two.Class1::load();
also works.
I found this info for you: http://stackoverflow.com/questions/411103/function-with-same-name-but-different-signature-in-derived-class
shortly: "It is because name lookup stops if it finds a name in one of your bases. It won't look beyond in other bases. The function in B shadows the function in A." there is more info.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

pgnatyukCommented:
We posted almost in the same time. :) So my last comment explains it?
0
itsmeandnobodyelseCommented:
>>>> two.Class1::load();

Yes, that's the way to go if you really think you need to have different signatures of load in baseclass and derived class.

I would name non-virtual functions of baseclass and derived class differently. Then a derived object or a pointer to derived can access all member functions both from baseclass and derived class and a baseclass pointer or baseclass object only those from the baseclass.

Overloading should be used only for virtual functions. Here calling the function for a derived object always will call the derived overload and if you want to call the basclass function you need to tell that explicitly by naming the class scope.

 
0
JohnGabyAuthor Commented:
It looks like it is by design then.  I guess I will have to live with it, although I don't understand the logic behind it.  If I am forced to use a cast, then later if I decided that I needed to add a load() to Class2 to overwrite the load() from Class1, it would not work, because of the casts.  I guess that I am simply going to have to change the name to be safe.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.