Overriding a Base’s Overloaded Function in C++ [duplicate]

In class bar, add

using foo::a;

This is a common ‘gotcha’ in C++. Once a name match is found in the a class scope, it doesn’t look further up the inheritance tree for overloads. By specifying the ‘using’ declaration, you bring all of the overloads of ‘a’ from ‘foo’ into the scope of ‘bar’. Then overloading works properly.

Keep in mind that if there is existing code using the ‘foo’ class, its meaning could be changed by the additional overloads. Or the additional overloads could introduce ambiguity and and the code will fail to compile. This is pointed out in James Hopkin’s answer.

Leave a Comment