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*)
:
- call scope and reference scope of the expression b->a are determined:
- call scope is the function f(B*).
- reference scope is class B.
2.The effective access of member a is determined:
- the original access of the member a is public in class A, the initial effective access of a is public.
- B inherits from A privately, the effective access of a inside class B is private.
- 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.