

C++ name hiding puzzle - sonic0002
http://pixelstech.net/article/index.php?id=1336894286

======
phzbOx
Actually, it's very well explained in one of the "Effective C++" series.

IIRC, when calling a function, the compiler first looks at the name and only
then will try to match the parameters (and choose the right function if
there're multiple ones).

So, in that case, it tries to find a function with the name "func" in the
Derivate class.. it finds one. Then, it tries to call it and fails because it
has the wrong argument. I.e. It won't look at the Base class since it already
found a function with the right name.

TL;DR: First = search by name, Second = Match parameters.

~~~
alok-g
This does not seem to explain it completely. Even with "first search by
name..." rule, shouldn't the order be:

1\. Find "all" the functions accessible bearing the same name. This would
include functions available from the base classes.

2\. Now match parameters

Seems like the order actually used is:

1\. Find the function by name in the specific class (not including base
classes)

2\. If found, go to 4.

3\. Include functions from the next base class (end if no more) and match by
name only. Go to 2.

4\. Try matching by name.

------
npsimons
Here I was hoping for a true mind-stretching puzzle to start the day/week, and
all I get is a rehash of things every C++ programmer should already know.

Is it something that catches people off guard the first time they experience
it? Yes. Does it not "make sense" to those who don't understand how C++
compilers work and (more importantly) _why_ they work that way? Yes.

As another poster said, this is already explained in "Effective C++", as well
as "Thinking in C++", and like TFA mentions the spec has it too. Yes, we all
know C++ is crufty; that's why they invented Java. But it's powerful and it's
flexible, and sometimes it's all we've got, so it's best if you learn to use
it properly if you're going to use it.

------
Someone
To my surprise, things can get funnier in the .NET world. There, you can mix
and match what C++ does with what this post expects:
[http://blogs.msdn.com/b/alexghi/archive/2010/01/27/visual-
ba...](http://blogs.msdn.com/b/alexghi/archive/2010/01/27/visual-basic-hides-
virtual-methods-from-c-not-really.aspx)

~~~
gecko
There's a big difference, though: in C#, you can't accidentally mask a base-
class function; you have to do so explicitly. If you fail to tell the compiler
whether you're overriding, hiding, or replacing, then it's a compilation
error. That's a very different beast than the C++ issues discussed in this
article.

~~~
Someone
That is true, but I still hold that .NET is more complex than C++. With C++,
the language made a choice (unfortunately, probably not the best one). With
.NET, one can encounter both choices in a single program. "Don't do that" is
not suffient here; the two choices might have been made in different third
party implementations of a common interface (let's say logger subclass A hides
Log(char) in the base class with a Log(int), while subclass B overloads it
with a Log(int))

Far-sought? Probably, but still surprisingly complex for a language that tries
to be simpler than C++.

------
ajross
FWIW, gcc 4.6.3 has no trouble with this code (or my hand-typed simplified
variant). So is it incompliant or just not subject to a bug that affects the
spec and (presumably) MSVC?

------
tfinniga
I do wish c++ had a keyword designating a function as implementing a base-
class virtual function, similar to C#'s override. Then this kind of thing
wouldn't be necessary.

~~~
Glowbox
C++11 has override and final
[http://en.wikipedia.org/wiki/C%2B%2B11#Explicit_overrides_an...](http://en.wikipedia.org/wiki/C%2B%2B11#Explicit_overrides_and_final)

------
alainbryden
Seems like a good question for stackoverflow

~~~
rmserror
Yeah, in fact, it's already there:

[http://stackoverflow.com/questions/4146499/why-does-a-
virtua...](http://stackoverflow.com/questions/4146499/why-does-a-virtual-
function-get-hidden)

