I am stumped by why the declaration of void operator()(int) in the base class in the code sample below is seemingly hidden when the derived class implements void operator()(int,int,int). How can I get the declaration of operator()(int) from the base class foo to be visible in the derived class bar? That is, how can I modify the example so that the invocation of operator()(int) works?
#include <iostream>
struct foo
{
void operator()(int)
{
std::cout << "A" << std::endl;
}
};
struct bar : foo
{
// If this is uncommented, the code will not compile.
// void operator()(int, int, int) {}
};
int main()
{
bar b;
b(1);
return 0;
}
When compiled with g++ with the marked lines uncommented, the error message is along the lines of “no match for call to ‘bar (int)’ … candidate is void bar::operator()(int,int,int) … candidate expects 3 arguments, 1 provided.”
That’s right. Derived class functions hide base class functions rather than overloading. The fix is pretty simple though: