Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 428
  • Last Modified:

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

0
JohnGaby
Asked:
JohnGaby
  • 3
  • 2
1 Solution
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now