Solved

Cannot access overloaded function which has no arguments

Posted on 2009-07-13
6
407 Views
Last Modified: 2013-12-14
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

0
Comment
Question by:JohnGaby
  • 3
  • 2
6 Comments
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 24846235
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
 
LVL 9

Author Comment

by:JohnGaby
ID: 24846400
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
 
LVL 33

Accepted Solution

by:
pgnatyuk earned 250 total points
ID: 24846409
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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 33

Expert Comment

by:pgnatyuk
ID: 24846418
We posted almost in the same time. :) So my last comment explains it?
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 24850103
>>>> 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
 
LVL 9

Author Closing Comment

by:JohnGaby
ID: 31603062
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

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
netbeans not showing web project 3 83
Java eclipse error 2 68
missing "main" in Java on Eclipse 4 70
The line on IDE 4 84
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question