(In the below article, geordi is used to test snippets. "BARK" is a geordi macro that outputs the function name of the enclosing scope.)

Here I briefly explore a common inheritance gotcha and its usual solution.

Normal overloading

A::lol(int) and A::lol(string) are actually different functions.

Overloading with inheritance

Here one might expect the second example to work, with the output "A::lol(3)" since the function clearly exists and should be inherited.

But in our example, B::lol(string) hides or "shadows" A::lol(int) so the lookup doesn't work. Since there is the function with the name "lol" in B, when you try to call b.lol(3) the compiler never bothers to look for other functions with the name "lol" that might be in A.

We could be a bit more explicit with our function call:

But that's a bit minging. It would be nicer to fix the lookup from within the class definition itself. To do so, we must bring A::lol(int) into scope of B with the using keyword so that the two functions lol(string) and lol(int) can live side by side in harmony.

With 'using'

It doesn't matter where you put the 'using' statement; it won't ever cancel out B::lol(string), even if it comes after the function definition:

Implicit conversions don't break this either:

So there you go.


Update 05/05/2011: Here's a really good explanation of the cause behind this shadowing.