
Signature
Doug Harrison
Visual C++ MVP
>>> But the added "top-level" const is not part of the function's signature,
>>> so the code is perfectly legit.
[quoted text clipped - 3 lines]
> Try the example I posted in another message. It demonstrates that
> differences in top-level const cause the function not to be overridden.
Yikes.
> That's a bug.
Definitely! I thought it was just an overzealous warning, not an
indication that the compiler was not considering Sub::f an override of
Base::f.
The warning is actually wrong in several ways:
> a.cpp(16) : warning C4301: 'Derived::Func': overriding virtual function
> only differs from 'Base::Func' by const/volatile qualifier
>
> The "only differs" bit makes it sound like it needs to differ by more than
> that, which makes no sense.
That's not the only way of interpreting it though. Either way, it would
alert you to the fact that Derived::Func is an override for Base::Func,
despite the top-level const that's been added. This may or may not have
been what the code writer intended.
It incorrectly identifies the function as an
> "overriding virtual function", when it doesn't override the base class
> version.
Well, in that respect, the warning text is correct, since it is
*supposed* to override the base, and a (separate?) compiler bug means it
doesn't.
It also is not a (new) virtual function.
Not sure what you mean there - it certainly is a virtual function.
>> I suppose it is warning you
>> that your added const is a bit of a strange thing to add in a function
[quoted text clipped - 4 lines]
> warning. Differences in top-level const should have exactly the same effect
> on the type of the function: none.
I agree, but if it were only a warning (and the compiler were making
Sub::Foo an override of Base::foo), I would not consider it a bug, but
rather an overzealous warning.
Tom
Doug Harrison [MVP] - 13 Nov 2006 18:16 GMT
>> It also is not a (new) virtual function.
>
>Not sure what you mean there - it certainly is a virtual function.
We may be mixing up our examples. If you extend the example I posted to
derive a class from Derived, you can demonstrate that the function in
Derived is not virtual. As it is not declared virtual, this makes some
sense, because due to the compiler bug, it doesn't override the base class
function.
>> So does using a different name for a parameter, but that doesn't generate a
>> warning. Differences in top-level const should have exactly the same effect
[quoted text clipped - 3 lines]
>Sub::Foo an override of Base::foo), I would not consider it a bug, but
>rather an overzealous warning.
I'd rather it warn about differences in default parameter values, or using
default parameters in virtual functions at all. :)
Here is an updated example that derives a new class Derived2 from Derived
and declares all its functions virtual:
#include <stdio.h>
class Base {
public:
virtual void Func(int i) {
puts("base");
}
};
class Derived : public Base {
public:
virtual void Func(const int i) { puts("derived"); }
};
class Derived2 : public Derived {
public:
//virtual void Func(int i) { puts("derived2"); }
virtual void Func(const int i) { puts("derived2"); }
};
int main() {
Derived2 d2;
Base* pBase = &d2;
Derived* pDerived = &d2;
pBase->Func(2);
pDerived->Func(2);
}
If you play around with this, you'll see the compiler considers "int" and
"const int" distinguishable for overriding purposes but not for
overloading, when they should be indistinguishable for both.

Signature
Doug Harrison
Visual C++ MVP