Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Cannot access overloaded function which has no arguments

Posted on 2009-07-13
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
	int load(void)
	int loadx(void)
class Class2 : public Class1
	int load(int arg)
int _tmain(int argc, _TCHAR* argv[])
	Class2 * p = new Class2();
	static_cast<Class1 *>(p)->load();
	return 0;

Open in new window

Question by:JohnGaby
  • 3
  • 2
LVL 33

Expert Comment

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;

Author Comment

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.
LVL 33

Accepted Solution

pgnatyuk earned 250 total points
ID: 24846409
One more thing:
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.
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

LVL 33

Expert Comment

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

Expert Comment

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.


Author Closing Comment

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.

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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ā€¦
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.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

792 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