Click here to Skip to main content
15,031,368 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Why is cbase::foobar() not found when called on type cderived while all members of cbase are declared public? This suggests name only is utilized for lookup and signature ignored but that is difficult to believe as one of the benefits of C++ is function overloading and selection based on argument type I would greately appreciate a reference to the language rule at cppreference.com explaining this Thank You Kindly

struct cbase
{
	void foobar() {}
};

struct cderived : public cbase
{
	void foobar(int) {}
};

int main()
{
	cderived d;
	d.foobar(); //'cderived::foobar': function does not take 0 arguments
}


What I have tried:

I tried waving a dead rubber chicken over the computer but that did not help I also tried reading cppreference.com re/ name lookup but did not find a rule explaining the compile error I also tried using cbase::foobar in cderived which solved the problem but I do not understand why it would be necessary
Posted
Updated 4-Jul-21 22:25pm
v2
Comments
[no name] 4-Jul-21 3:39am
   
Have a look to the compiler warnings (I hope they are enabled and that the chicken not picked them :-) ).
Most probably you will find something like cderived :: foobar(int) hides cbase :: foobar()
[no name] 4-Jul-21 3:59am
   
Have a look here and dearch for 'hides': Standard C++[^]

Variable d is an instantiation of a cderived class. So any call to foobar requires an integer to be passed to it, as per the signature of the function in that class. In order to call the base version you need to cast d to a cbase object thus:
C++
cderived d;
static_cast<cbase>(d).foobar(); // call the base function that takes 0 arguments
   
My thanks to dead.rubber.chicken for directing me to the language rule which I must accept so will utilize using directive to solve problem but still do not understand why the language refuses to support what seems a logical extension to the concept of overloading
   
Comments
[no name] 4-Jul-21 4:33am
   
As you wrote, this seems simply to be a rule.

Especially because the compiler recognizes that there is another - better- match (and therefore the warning) I recognize no reason why not the method of base class will be taken.

Maybe somebody other can explain the 'why'
[no name] 4-Jul-21 9:47am
   
It won't let me go, thank you;)
There seems to be no real explanation that the c ++ compiler won't accept this. The same example (inheritance and method names) work e.g. with C # as you / I would expect.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900