I have a situation where I am using a public API and need to "override" a function in a parent class, however this function has not been declared as virtual. Although it is hacky, I have decided that I am going to change the visibility of the parent class function using the code mentioned here: a way in c++ to hide a specific function.
I am, however, facing an issue, in that the parent class has an overload of this function with very similar parameters, and I am therefore getting the error "ambiguous call to overloaded function", even though I have made the parent class's function's scope private in my child class. I have simplified the solution below to illustrate the problem:
class Parent
{
public:
void doSomething(void* pointer) {}
};
class Child : public Parent
{
public:
void doSomething(const char* pointer) {}
private:
using Parent::doSomething;
};
int main()
{
Child childInstance;
childInstance.doSomething(nullptr); // error: 'Parent::doSomething': ambiguous call to overloaded function
}
Is this a bug? If not, how do I get around this? I am confused as to why the compiler is even searching in the parent class's namespace, when I have explicitly declared doSomething()
as private?
I am using MSVC 2019.
I do NOT want to do either of the following:
- Rename my subclass function (as this will cause inconsistency for me)
- Make my inheritance private and manually make public the functions I need (as the inheritance tree is extremely big and this would require making numerous functions in grandparent classes public too and so on, which is unsustainable)