Example of Access Resolution

The following example demonstrates the access resolution procedure.

class A
{
public:
      int a;
};
class B : private A
{
      friend void f (B*);
};
void f(B* b)
{
      b->a = 10; // is 'a' accessible to f(B*) ?
}
// ...

The following steps occur to determine the accessibility of A::a in f(B*) :

  1. call scope and reference scope of the expression b->a are determined:
    1. call scope is the function f(B*).
    2. reference scope is class B.
    2.The effective access of member a is determined:
    1. the original access of the member a is public in class A, the initial effective access of a is public.
    2. B inherits from A privately, the effective access of a inside class B is private.
    3. class B is the reference scope, the effective access procedure stops here. The effective access of a is private.
    3.The access rules are applied. The rules state that a private member can be accessed by a friend or a member of the member 9;s class. Because f(B*) is a friend of class B, f(B*) can access the private member a.