### Virtual Function Access

The access for a virtual function is specified when it is declared. The access rules for a virtual function are not affected by the access rules for the function that later overrides the virtual function. In general, the access of the overriding member function is not known.

If a virtual function is called with a pointer or reference to a class object, the type of the class object is not used to determine the access of the virtual function. Instead, the type of the pointer or reference to the class object is used.

In the following example, when the function f() is called using a pointer having type B* , bptr is used to determine the access to the function f(). Although the definition of f() defined in class D is executed, the access of the member function f() in class B is used. When the function f() is called using a pointer having type D*, dptr is used to determine the access to the function f(). This call produces an error because f() is declared private in class D.

    class B
    {
    public:
          virtual void f();
    };
    class D : public B
    {
    private:
          void f();
    };
    void main ()
    {
          D dobj;
          B *bptr = &dobj;
          D *dptr = &dobj;
          bptr->f();   // valid, virtual B::f() is public,
                       // D::f() is called
          dptr->f();   // error, D::f() is private
    }

Reference

Virtual Functions
Ambiguous Virtual Function Calls
Inherited Member Access
Pointer