%!TEX root = std.tex
\rSec0[class.access]{Member access control}%
\indextext{access control|(}

\indextext{protection|see{access control}}
\indextext{\idxcode{private}|see{access control, \tcode{private}}}
\indextext{\idxcode{protected}|see{access control, \tcode{protected}}}
\indextext{\idxcode{public}|see{access control, \tcode{public}}}

\pnum
A member of a class can be
\begin{itemize}
\item
\indextext{access control!\idxcode{private}}%
\tcode{private};
that is, its name can be used only by members and friends
of the class in which it is declared.
\item
\indextext{access control!\idxcode{protected}}%
\tcode{protected};
that is, its name can be used only by members and friends
of the class in which it is declared, by classes derived from that class, and by their
friends (see~\ref{class.protected}).
\item
\indextext{access control!\idxcode{public}}%
\tcode{public};
that is, its name can be used anywhere without access restriction.
\end{itemize}

\pnum
A member of a class can also access all the names to which the class has access.
A local class of a member function may access
the same names that the member function itself may access.\footnote{Access
permissions are thus transitive and cumulative to nested
and local classes.}

\pnum
\indextext{access control!member name}%
\indextext{default access control|see{access control, default}}%
\indextext{access control!default}%
Members of a class defined with the keyword
\tcode{class}
are
\tcode{private}
by default.
Members of a class defined with the keywords
\tcode{struct}
or
\tcode{union}
are
\tcode{public}
by default.
\enterexample

\begin{codeblock}
class X {
  int a;            // \tcode{X::a} is private by default
};

struct S {
  int a;            // \tcode{S::a} is public by default
};
\end{codeblock}
\exitexample

\pnum
Access control is applied uniformly to all names, whether the names are
referred to from declarations or expressions.
\enternote
Access control applies to names nominated by
\tcode{friend}
declarations~(\ref{class.friend}) and
\grammarterm{using-declaration}{s}~(\ref{namespace.udecl}).
\exitnote
In the case of overloaded function names, access control is applied to
the function selected by overload resolution.
\enternote
Because access control applies to names, if access control is applied to a
typedef name, only the accessibility of the typedef name itself is considered.
The accessibility of the entity referred to by the typedef is not considered.
For example,

\begin{codeblock}
class A {
  class B { };
public:
  typedef B BB;
};

void f() {
  A::BB x;          // OK, typedef name \tcode{A::BB} is public
  A::B y;           // access error, \tcode{A::B} is private
}
\end{codeblock}
\exitnote

\pnum
It should be noted that it is
\term{access}
to members and base classes that is controlled, not their
\term{visibility}.
Names of members are still visible, and implicit conversions to base
classes are still considered, when those members and base classes are
inaccessible.
The interpretation of a given construct is
established without regard to access control.
If the interpretation
established makes use of inaccessible member names or base classes,
the construct is ill-formed.

\pnum
All access controls in Clause~\ref{class.access} affect the ability to access a class member
name from the declaration of a particular
entity, including parts of the declaration preceding the name of the entity
being declared and, if the entity is a class, the definitions of members of
the class appearing outside the class's \grammarterm{member-specification}{.}
\enternote this access also applies to implicit references to constructors,
conversion functions, and destructors. \exitnote
\enterexample

\begin{codeblock}
class A {
  typedef int I;    // private member
  I f();
  friend I g(I);
  static I x;
  template<int> struct Q;
  template<int> friend struct R;
protected:
    struct B { };
};

A::I A::f() { return 0; }
A::I g(A::I p = A::x);
A::I g(A::I p) { return 0; }
A::I A::x = 0;
template<A::I> struct A::Q { };
template<A::I> struct R { };

struct D: A::B, A { };
\end{codeblock}

\pnum
Here, all the uses of
\tcode{A::I}
are well-formed because
\tcode{A::f},
\tcode{A::x}, and \tcode{A::Q}
are members of class
\tcode{A}
and
\tcode{g}
and \tcode{R} are friends of class
\tcode{A}.
This implies, for example, that access checking on the first use of
\tcode{A::I}
must be deferred until it is determined that this use of
\tcode{A::I}
is as the return type of a member of class
\tcode{A}.
Similarly, the use of \tcode{A::B} as a
\grammarterm{base-specifier} is well-formed because \tcode{D}
is derived from \tcode{A}, so checking of \grammarterm{base-specifier}{s}
must be deferred until the entire \grammarterm{base-specifier-list} has been seen.
\exitexample

\pnum
\indextext{argument!access checking~and default}%
\indextext{access control!default argument}%
The names in a default argument~(\ref{dcl.fct.default}) are
bound at the point of declaration, and access is checked at that
point rather than at any points of use of the default argument.
Access checking for default arguments in function templates and in
member functions of class templates is performed as described in~\ref{temp.inst}.

\pnum
The names in a default \grammarterm{template-argument}~(\ref{temp.param})
have their access checked in the context in which they appear rather than at any
points of use of the default \grammarterm{template-argument}. \enterexample
\begin{codeblock}
class B { };
template <class T> class C {
protected:
  typedef T TT;
};

template <class U, class V = typename U::TT>
class D : public U { };

D <C<B> >* d;       // access error, C::TT is protected
\end{codeblock}
\exitexample

\rSec1[class.access.spec]{Access specifiers}%
\indextext{access~specifier}

\pnum
Member declarations can be labeled by an
\grammarterm{access-specifier}
(Clause~\ref{class.derived}):

\begin{ncbnftab}
access-specifier \terminal{:} member-specification\opt
\end{ncbnftab}

An
\grammarterm{access-specifier}
specifies the access rules for members following it
until the end of the class or until another
\grammarterm{access-specifier}
is encountered.
\enterexample

\begin{codeblock}
class X {
  int a;            // \tcode{X::a} is private by default: \tcode{class} used
public:
  int b;            // \tcode{X::b} is public
  int c;            // \tcode{X::c} is public
};
\end{codeblock}
\exitexample

\pnum
Any number of access specifiers is allowed and no particular order is required.
\enterexample

\begin{codeblock}
struct S {
  int a;            // \tcode{S::a} is public by default: \tcode{struct} used
protected:
  int b;            // \tcode{S::b} is protected
private:
  int c;            // \tcode{S::c} is private
public:
  int d;            // \tcode{S::d} is public
};
\end{codeblock}
\exitexample

\pnum
\enternote The effect of access control on the order of allocation
of data members is described in~\ref{class.mem}.\exitnote

\pnum
When a member is redeclared within its class definition,
the access specified at its redeclaration shall
be the same as at its initial declaration.
\enterexample

\begin{codeblock}
struct S {
  class A;
  enum E : int;
private:
  class A { };        // error: cannot change access
  enum E: int { e0 }; // error: cannot change access
};
\end{codeblock}
\exitexample

\pnum
\enternote
In a derived class, the lookup of a base class name will find the
injected-class-name instead of the name of the base class in the scope
in which it was declared. The injected-class-name might be less accessible
than the name of the base class in the scope in which it was declared.
\exitnote

\enterexample
\begin{codeblock}
class A { };
class B : private A { };
class C : public B {
  A* p;             // error: injected-class-name \tcode{A} is inaccessible
  ::A* q;           // OK
};
\end{codeblock}
\exitexample

\rSec1[class.access.base]{Accessibility of base classes and base class members}%
\indextext{access control!base~class}%
\indextext{access~specifier}%
\indextext{base~class!\idxcode{private}}%
\indextext{base~class!\idxcode{protected}}%
\indextext{base~class!\idxcode{public}}

\pnum
If a class is declared to be a base class (Clause~\ref{class.derived}) for another class using the
\tcode{public}
access specifier, the
\tcode{public}
members of the base class are accessible as
\tcode{public}
members of the derived class and
\tcode{protected}
members of the base class are accessible as
\tcode{protected}
members of the derived class.
If a class is declared to be a base class for another class using the
\tcode{protected}
access specifier, the
\tcode{public}
and
\tcode{protected}
members of the base class are accessible as
\tcode{protected}
members of the derived class.
If a class is declared to be a base class for another class using the
\tcode{private}
access specifier, the
\tcode{public}
and
\tcode{protected}
members of the base class are accessible as
\tcode{private}
members of the derived class\footnote{As specified previously in Clause~\ref{class.access},
private members of a base class remain inaccessible even to derived classes
unless
\tcode{friend}
declarations within the base class definition are used to grant access explicitly.}.

\pnum
In the absence of an
\grammarterm{access-specifier}
for a base class,
\tcode{public}
is assumed when the derived class is
defined with the \grammarterm{class-key}
\tcode{struct}
and
\tcode{private}
is assumed when the class is
defined with the \grammarterm{class-key}
\tcode{class}.
\enterexample

\begin{codeblock}
class B { /* ... */ };
class D1 : private B { /* ... */ };
class D2 : public B { /* ... */ };
class D3 : B { /* ... */ };     // \tcode{B} private by default
struct D4 : public B { /* ... */ };
struct D5 : private B { /* ... */ };
struct D6 : B { /* ... */ };    // \tcode{B} public by default
class D7 : protected B { /* ... */ };
struct D8 : protected B { /* ... */ };
\end{codeblock}

Here
\tcode{B}
is a public base of
\tcode{D2},
\tcode{D4},
and
\tcode{D6},
a private base of
\tcode{D1},
\tcode{D3},
and
\tcode{D5},
and a protected base of
\tcode{D7}
and
\tcode{D8}.
\exitexample

\pnum
\enternote
A member of a private base class might be inaccessible as an inherited
member name, but accessible directly.
Because of the rules on pointer conversions~(\ref{conv.ptr}) and explicit casts~(\ref{expr.cast}), a conversion from a pointer to a derived class to a pointer
to an inaccessible base class might be ill-formed if an implicit conversion
is used, but well-formed if an explicit cast is used.
For example,

\begin{codeblock}
class B {
public:
  int mi;                       // non-static member
  static int si;                // static member
};
class D : private B {
};
class DD : public D {
  void f();
};

void DD::f() {
  mi = 3;                       // error: \tcode{mi} is private in \tcode{D}
  si = 3;                       // error: \tcode{si} is private in \tcode{D}
  ::B  b;
  b.mi = 3;                     // OK (\tcode{b.mi} is different from \tcode{this->mi})
  b.si = 3;                     // OK (\tcode{b.si} is different from \tcode{this->si})
  ::B::si = 3;                  // OK
  ::B* bp1 = this;              // error: \tcode{B} is a private base class
  ::B* bp2 = (::B*)this;        // OK with cast
  bp2->mi = 3;                  // OK: access through a pointer to \tcode{B}.
}
\end{codeblock}
\exitnote

\pnum
A base class
\tcode{B}
of
\tcode{N}
is
\term{accessible}
at
\term{R},
if
\begin{itemize}
\item
an invented public member of
\tcode{B}
would be a public member of
\tcode{N}, or
\item
\term{R}
occurs in a member or friend of class
\tcode{N},
and an invented public member of
\tcode{B}
would be a private or protected member of
\tcode{N}, or
\item
\term{R}
occurs in a member or friend of a class
\tcode{P}
derived from
\tcode{N},
and an invented public member of
\tcode{B}
would be a private or protected member of
\tcode{P}, or
\item
there exists a class
\tcode{S}
such that
\tcode{B}
is a base class of
\tcode{S}
accessible at
\term{R}
and
\tcode{S}
is a base class of
\tcode{N}
accessible at
\term{R}.
\end{itemize}

\enterexample
\begin{codeblock}
class B {
public:
  int m;
};

class S: private B {
  friend class N;
};

class N: private S {
  void f() {
    B* p = this;    // OK because class \tcode{S} satisfies the fourth condition
                    // above: \tcode{B} is a base class of \tcode{N} accessible in \tcode{f()} because
                    // \tcode{B} is an accessible base class of \tcode{S} and \tcode{S} is an accessible
                    // base class of \tcode{N}.
  }
};
\end{codeblock}
\exitexample

\pnum
If a base class is accessible, one can implicitly convert a pointer to
a derived class to a pointer to that base class~(\ref{conv.ptr}, \ref{conv.mem}).
\enternote
It follows that
members and friends of a class
\tcode{X}
can implicitly convert an
\tcode{X*}
to a pointer to a private or protected immediate base class of
\tcode{X}.
\exitnote
The access to a member is affected by the class in which the member is
named.
This naming class is the class in which the member name was looked
up and found.
\enternote
This class can be explicit, e.g., when a
\grammarterm{qualified-id}
is used, or implicit, e.g., when a class member access operator~(\ref{expr.ref}) is used (including cases where an implicit
``\tcode{this->}''
is
added).
If both a class member access operator and a
\grammarterm{qualified-id}
are used to name the member (as in
\tcode{p->T::m}),
the class naming the member is the class denoted by the
\grammarterm{nested-name-specifier}
of the
\grammarterm{qualified-id}
(that is,
\tcode{T}).
\exitnote
A member
\tcode{m}
is accessible at the point
\term{R}
when named in class
\tcode{N}
if
\begin{itemize}
\item
\tcode{m}
as a member of
\tcode{N}
is public, or
\item
\tcode{m}
as a member of
\tcode{N}
is private, and
\term{R}
occurs in a member or friend of class
\tcode{N},
or
\item
\tcode{m}
as a member of
\tcode{N}
is protected, and
\term{R}
occurs in a member or friend of class
\tcode{N},
or in a member or friend of a class
\tcode{P}
derived from
\tcode{N},
where
\tcode{m}
as a member of
\tcode{P}
is public, private, or protected, or
\item
there exists a base class
\tcode{B}
of
\tcode{N}
that is accessible at
\term{R},
and
\tcode{m}
is accessible at
\term{R}
when named in class
\tcode{B}.
\enterexample

\begin{codeblock}
class B;
class A {
private:
  int i;
  friend void f(B*);
};
class B : public A { };
void f(B* p) {
  p->i = 1;         // OK: \tcode{B*} can be implicitly converted to \tcode{A*},
                    // and \tcode{f} has access to \tcode{i} in \tcode{A}
}
\end{codeblock}
\exitexample
\end{itemize}

\pnum
If a class member access operator, including an implicit
``\tcode{this->},''
is used to access a non-static data member or non-static
member function, the reference is ill-formed if the
left operand (considered as a pointer in the
``\tcode{.}''
operator case) cannot be implicitly converted to a
pointer to the naming class of the right operand.
\enternote
This requirement is in addition to the requirement that
the member be accessible as named.
\exitnote

\rSec1[class.friend]{Friends}%
\indextext{friend function!access and}%
\indextext{access control!friend function}

\pnum
A friend of a class is a function or class that is
given permission to use the private and protected member names from the class.
A class specifies its friends, if any, by way of friend declarations.
Such declarations give special access rights to the friends, but they
do not make the nominated friends members of the befriending class.
\enterexample
the following example illustrates the differences between
members and friends:
\indextext{friend function!member~function~and}%
\indextext{example!friend function}%
\indextext{example!member~function}%

\begin{codeblock}
class X {
  int a;
  friend void friend_set(X*, int);
public:
  void member_set(int);
};

void friend_set(X* p, int i) { p->a = i; }
void X::member_set(int i) { a = i; }

void f() {
  X obj;
  friend_set(&obj,10);
  obj.member_set(10);
}
\end{codeblock}
\exitexample

\pnum
\indextext{friend!class access~and}%
Declaring a class to be a friend implies that the names of private and
protected members from the class granting friendship can be accessed in the
\grammarterm{base-specifier}{s} and member declarations of the befriended
class. \enterexample

\begin{codeblock}
class A {
  class B { };
  friend class X;
};

struct X : A::B {   // OK: \tcode{A::B} accessible to friend
  A::B mx;          // OK: \tcode{A::B} accessible to member of friend
  class Y {
    A::B my;        // OK: \tcode{A::B} accessible to nested member of friend
  };
};
\end{codeblock}
\exitexample
\enterexample

\begin{codeblock}
class X {
  enum { a=100 };
  friend class Y;
};

class Y {
  int v[X::a];      // OK, \tcode{Y} is a friend of \tcode{X}
};

class Z {
  int v[X::a];      // error: \tcode{X::a} is private
};
\end{codeblock}
\exitexample

A class shall not be defined in a friend declaration. \enterexample
\begin{codeblock}
class A {
  friend class B { }; // error: cannot define class in friend declaration
};
\end{codeblock}
\exitexample

\pnum
A \tcode{friend} declaration that does not declare a function
shall have one of the following forms:

\begin{ncsimplebnf}
\terminal{friend} elaborated-type-specifier \terminal{;}\br
\terminal{friend} simple-type-specifier \terminal{;}\br
\terminal{friend} typename-specifier \terminal{;}
\end{ncsimplebnf}

\enternote A \tcode{friend} declaration may be the
\term{declaration} in a \grammarterm{template-declaration}
(Clause~\ref{temp}, \ref{temp.friend}).\exitnote If the
type specifier in a \tcode{friend} declaration designates a (possibly
cv-qualified) class type, that class is declared as a friend; otherwise, the
\tcode{friend} declaration is ignored. \enterexample

\begin{codeblock}
class C;
typedef C Ct;

class X1 {
  friend C;         // OK: \tcode{class C} is a friend
};

class X2 {
  friend Ct;        // OK: \tcode{class C} is a friend
  friend D;         // error: no type-name \tcode{D} in scope
  friend class D;   // OK: elaborated-type-specifier declares new class
};

template <typename T> class R {
  friend T;
};

R<C> rc;            // \tcode{class C} is a friend of \tcode{R<C>}
R<int> Ri;          // OK: \tcode{"friend int;"} is ignored
\end{codeblock}
\exitexample

\pnum
\indextext{friend function!linkage~of}%
A function first declared in a friend declaration
has external linkage~(\ref{basic.link}).
Otherwise, the function retains its previous linkage~(\ref{dcl.stc}).

\pnum
\indextext{declaration!overloaded~name~and \tcode{friend}}%
When a
\tcode{friend}
declaration refers to an overloaded name or operator, only the function specified
by the parameter types becomes a friend.
A member function of a class
\tcode{X}
can be a friend of
a class
\tcode{Y}.
\indextext{member function!friend}%
\enterexample

\begin{codeblock}
class Y {
  friend char* X::foo(int);
  friend X::X(char);            // constructors can be friends
  friend X::~X();               // destructors can be friends
};
\end{codeblock}
\exitexample

\pnum
\indextext{friend function!inline}%
A function can be defined in a friend declaration of a class if and only if the
class is a non-local class~(\ref{class.local}), the function name is unqualified,
and the function has namespace scope.
\enterexample

\begin{codeblock}
class M {
  friend void f() { }           // definition of global \tcode{f}, a friend of \tcode{M},
                                // not the definition of a member function
};
\end{codeblock}
\exitexample

\pnum
Such a function is implicitly
\tcode{inline}.
A
\tcode{friend}
function defined in a class is in the (lexical) scope of the class in which it is defined.
A friend function defined outside the class is not~(\ref{basic.lookup.unqual}).

\pnum
No
\grammarterm{storage-class-specifier}
shall appear in the
\grammarterm{decl-specifier-seq}
of a friend declaration.

\pnum
\indextext{friend!access~specifier~and}%
A name nominated by a friend declaration shall be accessible in the scope of the
class containing the friend declaration.
The meaning of the friend declaration is the same whether the friend declaration
appears in the
\tcode{private},
\tcode{protected}
or
\tcode{public}~(\ref{class.mem})
portion of the class
\grammarterm{member-specification}.

\pnum
\indextext{friend!inheritance~and}%
Friendship is neither inherited nor transitive.
\enterexample

\begin{codeblock}
class A {
  friend class B;
  int a;
};

class B {
  friend class C;
};

class C  {
  void f(A* p) {
    p->a++;         // error: \tcode{C} is not a friend of \tcode{A}
                    // despite being a friend of a friend
  }
};

class D : public B  {
  void f(A* p) {
    p->a++;         // error: \tcode{D} is not a friend of \tcode{A}
                    // despite being derived from a friend
  }
};
\end{codeblock}
\exitexample

\pnum
\indextext{local~class!friend}%
\indextext{friend!local~class~and}%
If a friend declaration appears in a local class~(\ref{class.local}) and the
name specified is an unqualified name, a prior declaration is looked
up without considering scopes that are outside the innermost enclosing
non-class scope.
For a friend function declaration, if there is no
prior declaration, the program is ill-formed.
For a friend class
declaration, if there is no prior declaration, the class that is
specified belongs to the innermost enclosing non-class scope, but if it is
subsequently referenced, its name is not found by name lookup
until a matching declaration is provided in the innermost enclosing
non-class scope.
\enterexample

\begin{codeblock}
class X;
void a();
void f() {
  class Y;
  extern void b();
  class A {
  friend class X;   // OK, but \tcode{X} is a local class, not \tcode{::X}
  friend class Y;   // OK
  friend class Z;   // OK, introduces local class \tcode{Z}
  friend void a();  // error, \tcode{::a} is not considered
  friend void b();  // OK
  friend void c();  // error
  };
  X* px;            // OK, but \tcode{::X} is found
  Z* pz;            // error, no \tcode{Z} is found
}
\end{codeblock}
\exitexample

\rSec1[class.protected]{Protected member access}
\indextext{access control!\idxcode{protected}}%

\pnum
An additional access check beyond those described earlier in Clause~\ref{class.access}
is applied when a non-static data member or non-static member function is a
protected member of its naming class~(\ref{class.access.base})\footnote{This
additional check does not apply to other members,
e.g., static data members or enumerator member constants.}
As described earlier, access to a protected member is granted because the
reference occurs in a friend or member of some class \tcode{C}. If the access is
to form a pointer to member~(\ref{expr.unary.op}), the
\grammarterm{nested-name-specifier} shall denote \tcode{C} or a class derived from
\tcode{C}. All other accesses involve a (possibly implicit) object
expression~(\ref{expr.ref}). In this case, the class of the object expression shall be
\tcode{C} or a class derived from \tcode{C}.
\enterexample

\begin{codeblock}
class B {
protected:
  int i;
  static int j;
};

class D1 : public B {
};

class D2 : public B {
  friend void fr(B*,D1*,D2*);
  void mem(B*,D1*);
};

void fr(B* pb, D1* p1, D2* p2) {
  pb->i = 1;                    // ill-formed
  p1->i = 2;                    // ill-formed
  p2->i = 3;                    // OK (access through a \tcode{D2})
  p2->B::i = 4;                 // OK (access through a \tcode{D2}, even though
                                // naming class is \tcode{B})
  int B::* pmi_B = &B::i;       // ill-formed
  int B::* pmi_B2 = &D2::i;     // OK (type of \tcode{\&D2::i} is \tcode{int B::*})
  B::j = 5;                     // OK (because refers to static member)
  D2::j = 6;                    // OK (because refers to static member)
}

void D2::mem(B* pb, D1* p1) {
  pb->i = 1;                    // ill-formed
  p1->i = 2;                    // ill-formed
  i = 3;                        // OK (access through \tcode{this})
  B::i = 4;                     // OK (access through \tcode{this}, qualification ignored)
  int B::* pmi_B = &B::i;       // ill-formed
  int B::* pmi_B2 = &D2::i;     // OK
  j = 5;                        // OK (because \tcode{j} refers to static member)
  B::j = 6;                     // OK (because \tcode{B::j} refers to static member)
}

void g(B* pb, D1* p1, D2* p2) {
  pb->i = 1;                    // ill-formed
  p1->i = 2;                    // ill-formed
  p2->i = 3;                    // ill-formed
}
\end{codeblock}
\exitexample

\rSec1[class.access.virt]{Access to virtual functions}%
\indextext{access control!virtual function}

\pnum
The access rules (Clause~\ref{class.access}) for a virtual function are determined by its declaration
and are not affected by the rules for a function that later overrides it.
\enterexample

\begin{codeblock}
class B {
public:
  virtual int f();
};

class D : public B {
private:
  int f();
};

void f() {
  D d;
  B* pb = &d;
  D* pd = &d;

  pb->f();                      // OK: \tcode{B::f()} is public,
                                // \tcode{D::f()} is invoked
  pd->f();                      // error: \tcode{D::f()} is private
}
\end{codeblock}
\exitexample

\pnum
Access is checked at the call point using the type of the expression used
to denote the object for which the member function is called
(\tcode{B*}
in the example above).
The access of the member function in the class in which it was defined
(\tcode{D}
in the example above) is in general not known.

\rSec1[class.paths]{Multiple access}%
\indextext{access control!multiple access}

\pnum
If a name can be reached by several paths through a multiple inheritance
graph, the access is that of the path that gives most access.
\enterexample

\begin{codeblock}
class W { public: void f(); };
class A : private virtual W { };
class B : public virtual W { };
class C : public A, public B {
  void f() { W::f(); }          // OK
};
\end{codeblock}

\pnum
Since
\tcode{W::f()}
is available to
\tcode{C::f()}
along the public path through
\tcode{B},
access is allowed.
\exitexample

\rSec1[class.access.nest]{Nested classes}%
\indextext{access control!nested class}%
\indextext{member function!nested class}

\pnum
A nested class is a member and as such has the same access rights as any other member.
The members of an enclosing class have no special access to members of a nested
class; the usual access rules (Clause~\ref{class.access}) shall be obeyed.
\enterexample
\indextext{example!nested~class definition}%

\begin{codeblock}
class E {
  int x;
  class B { };

  class I {
    B b;                        // OK: \tcode{E::I} can access \tcode{E::B}
    int y;
    void f(E* p, int i) {
      p->x = i;                 // OK: \tcode{E::I} can access \tcode{E::x}
    }
  };

  int g(I* p) {
    return p->y;                // error: \tcode{I::y} is private
  }
};
\end{codeblock}
\exitexample%
\indextext{access control|)}
