if
B::f(int)
hidesA::f()
, why doespa1->f();
not give an error?
Because pa1
points to A
, and A
has a member called f
which can be called like that. In this context, any other class (including B
) is irrelevant.
Doesn't name hiding mean that, the function
f()
doesn't exist in classB
?
No. It means that, in the context of B
, the only function called f
that can be found by unqualified lookup is B::f
. It doesn't remove f
from any other context, or prevent it from being found by qualified lookup such as b.A::f()
.
And if
pa1
points to an object of classB
thenpa1->f();
should result in an error just asb.f()
does!
The dynamic type is B
, so that's the type used (at run time) to call virtual functions. Non-virtual functions are selected by the compiler according to the static type, which is A
. In general, the compiler doesn't know the dynamic type; all it knows is that the pointer points to an A
or some unknown derived class.