Re: constness and inheritance
sndive@gmail.com wrote:
struct A {
virtual int foo(const int bar);
};
int A::foo(const int bar)
{
return 0;
}
struct B : public A {
int foo(int bar);
This 'foo' *overrides* 'A::foo' because it has exactly same type.
};
int B::foo(int bar)
{
return 1;
}
int
main()
{
A *p = new B;
const int baz=-1;
int r = p->foo(baz);
return 0;
}
here B::foo is called (tried with g++3.2.3)
As it bloody well should.
i wonder if it's a problem for non elementary types in parameters
since derived function can modify the baz argument with impunity
contrary to the expectations of the caller.
Top-level const qualifiers are ignored in the function declaration
as far as type matching is concerned. In your program 'B::foo'
and 'A::foo' have *exactly same* type.
Granted, B::foo could've taken bar as the const parameter
and casted to non const but there are no casts of any
kind in the program above.
The top-level 'const' doesn't matter.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask