Solved

Cannot access overloaded function which has no arguments

Posted on 2009-07-13
6
402 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

759 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now