C++ name hiding puzzle
pixelstech.net
pixelstech.net
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.
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.
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.
Far-sought? Probably, but still surprisingly complex for a language that tries to be simpler than C++.
http://stackoverflow.com/questions/4146499/why-does-a-virtua...