[/QUOTE]
First time I've heard him called Scotty
.
Item 48: Pay attention to compiler warnings. Actually I read
again and it doesn't say it's an error, just that some
compilers will emit a warning about name hiding.
Which is something completely different. There is no error in
the code, at least with regards to the standard. There is,
likely, an error with regards to what was wanted. Compilers are
free to warn about anything they feel like; in this case, the
warning is probably a good thing, since you probably meant to
make the derived function const as well (so that it would
override the function in the base class).
I was confused, I read again and it says some compilers emit a
warning about name hiding and others don't.
And it probably depends on the options with which you invoke the
compiler. Try using -Woverloaded-virtual with g++, for example.
(Don't know why this isn't included with -Wall, but it isn't.)
I found this is as ambigious as having
virtual void f(int x);
virtual void f(char x);
I think your confusing ambigious with some other word. There's
not the slightest ambiguity here; the standard is clear as to
what is required. (Note that ambiguous can easily have two
different meanings in this context: the standard itself can be
ambiguous, in which case, we can't determine exactly what is
required; or a function call can be abiguous: the standard
(clearly?) says that the call is ambiguous, and requires the
compiler to issue a diagnostic. Neither applies here, however.)
The rule is somewhat surprising here, but is necessary in its
general form to prevent code from silently changing meaning when
presumably unrelated code (e.g. the private section of a base
class) evolves. It also turns out to be very coherent, working
over classes in the same way it works inside functions, etc.
Roughly speaking, when doing name lookup, the compiler
estabishes an ordered list of scopes, and stops at the first one
in which it finds the name.
If I could change something here (but it's way too late), I'd
require some sort of explicit statement as to 1) whether this
declaration is meant to override something in the base class or
not, and 2) whether the author wishes to allow this declaration
to be overridden. Something like:
void f() ;
// overrides nothing, cannot be overridden
overrides void f() ;
// overrides a virtual function in a
// base class, cannot be overridden
virtual void f() ;
// overrides nothing, but can be
// overridden.
overrides virtual void f() ;
// overrides a virtual function in a
// base class, and can be overriden in
// a derived class.
Name hiding would still work as it does now, but if you meant to
override, and made a mistake in the signature, you would get a
compiler error (and not just maybe a warning).