Achintya wrote in news:1122128095.067742.271170
@g43g2000cwa.googlegroups.com in comp.lang.c++:
> int main()
> {
> A* a_base;
> B* b_derived;
>
> a_base = new B(1);
> a_base->f();
> }
> &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&&
>
> Hi,
>
> In the above code the virtual function declaration in A is causing a
> compiler error since its private. Does this mean that the virtual
> function overring requires the base delaration also to be public.
> Kindly explain...as i hadn't tried this earlier.
>
No, public is required as you call A::f() from main(), i.e. a
context where public access to A::f() is required.
When you make a call to A::f() the compiler doesn't care
wether A::f() is virtual, or if its been overriden, it only
checks the accessability of A::f().
The fact that in this case a_base actually points to a B with
an accessable f() doesen't matter. Type and access checking is
done by the compiler at compile time, not at runtime, and in
general the compiler doesn't know the dynamic type (B - above)
that a pointer (A *a_base - above) points too.
HTH.
Rob.
--
http://www.victim-prime.dsl.pipex.com/