Access Resolution
Access resolution is the process by which the accessibility of
a particular class member is determined. Accessibility is
dependent on the context. For example, a class member can be
accessible in a member function but inaccessible at file scope.
The following describes the access resolution procedure used by
the compiler.
In general, two scopes must be established before
access resolution is applied. These scopes reduce an expression
or declaration into a simplified construct to which the access
rules are applied. These scopes are:
Call scope: the scope that encloses the expression or
declaration that uses the class member.
Reference scope: the scope that identifies the class.
For example, in the following code:
// This example illustrates access resolution.
class B { public: int member; }; // declaration
class A : B {} // declaration
void main()
{
A aobject; // declaration
aobject.member = 10; // expression
}
the reference scope for member is the type of aobject, that is
class type A. Reference scope is chosen by simplifying the
expression (or declaration) containing the member. An expression
can be thought of as being reduced to a simple expression of the
form obj .member where obj is the reference scope. Reference
scope is selected as follows:
- If the member is qualified with . (dot) or -> (arrow),
the reference scope is the type of the object that is
immediately to the left of the . or -> operator
closest to the member. Unqualified members are treated as
if they are qualified with this->.
- If the member is a type member or a static member and is
qualified with :: (the scope resolution operator ), the
reference scope is the type immediately to the left of
the :: operator closest to the member.
- Otherwise, the reference scope is the call scope.
The call scope and the reference scope determine the
accessibility of a class member. Once these scopes are
resolved, the effective access of the member is
determined. Effective access is the access of the member
as it is seen from the reference scope. It is determined
by taking the original access of the member in its scope
as the effective access and changing it as the class
hierarchy is traversed from the member's class to the
reference scope. Effective access is altered as the class
hierarchy is traversed for each derivation by the
following:
- The derivation access of a base class
- Access declarations that are applied to the members
- Friendships that are granted to the call scope
After effective access is determined for a member, the
access rules are applied as if the effective access were
the original access of the member. A member is only
accessible if the access rules say that it is.
Example of Access Resolution
Derivation Access of Base Classes
Access Declarations
Member Access
Derivation
Overloading Functions
Scope Resolution Operator