[class.access.general]

# 11 Classes [[class]](./#class)

## 11.8 Member access control [[class.access]](class.access#general)

### 11.8.1 General [class.access.general]

[1](#1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L4323)

A member of a class can be

- [(1.1)](#1.1)

private,
that is, it can be named only by members and friends
of the class in which it is declared;

- [(1.2)](#1.2)

protected,
that is, it can be named only by members and friends
of the class in which it is declared, by classes derived from that class, and by their
friends (see [[class.protected]](class.protected "11.8.5 Protected member access")); or

- [(1.3)](#1.3)

public,
that is, it can be named anywhere without access restriction[.](#1.sentence-1)

[*Note [1](#note-1)*:

A constructor or destructor can be named by an expression ([[basic.def.odr]](basic.def.odr "6.3 One-definition rule"))
even though it has no name[.](#1.sentence-2)

â *end note*]

[2](#2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L4347)

A member of a class can also access all the members to which the class has access[.](#2.sentence-1)

A local class of a member function may access
the same members that the member function itself may access[.](#2.sentence-2)[96](#footnote-96 "Access permissions are thus transitive and cumulative to nested and local classes.")

[3](#3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L4357)

Members of a class defined with the keywordclass are private by default[.](#3.sentence-1)

Members of a class defined with the keywordsstruct or union are public by default[.](#3.sentence-2)

[*Example [1](#example-1)*: class X {int a; // X​::​a is private by default};

struct S {int a; // S​::​a is public by default}; â *end example*]

[4](#4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L4379)

Access control is applied uniformly to declarations and expressions[.](#4.sentence-1)

[*Note [2](#note-2)*:

Access control applies to members nominated by
friend declarations ([[class.friend]](class.friend "11.8.4 Friends")) and[*using-declaration*](namespace.udecl#nt:using-declaration "9.10 The using declaration [namespace.udecl]")*s* ([[namespace.udecl]](namespace.udecl "9.10 The using declaration"))[.](#4.sentence-2)

â *end note*]

When a [*using-declarator*](namespace.udecl#nt:using-declarator "9.10 The using declaration [namespace.udecl]") is named,
access control is applied to it, not to the declarations that replace it[.](#4.sentence-3)

For an overload set, access control is applied only to
the function selected by overload resolution[.](#4.sentence-4)

[*Example [2](#example-2)*: struct S {void f(int);private:void f(double);};

void g(S* sp) { sp->f(2); // OK, access control applied after overload resolution} â *end example*]

[*Note [3](#note-3)*:

Because access control applies to the declarations named,
if access control is applied to a type alias,
only the accessibility of the typedef or alias declaration itself is considered[.](#4.sentence-5)

The accessibility of the underlying entity is not considered[.](#4.sentence-6)

[*Example [3](#example-3)*: class A {class B { };public:typedef B BB;};

void f() { A::BB x; // OK, typedef A​::​BB is public A::B y; // access error, A​::​B is private} â *end example*]

â *end note*]

[5](#5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L4424)

[*Note [4](#note-4)*:

Access control does not prevent members from being found by name lookup or
implicit conversions to base classes from being considered[.](#5.sentence-1)

â *end note*]

The interpretation of a given construct is
established without regard to access control[.](#5.sentence-2)

If the interpretation
established makes use of inaccessible members or base classes,
the construct is ill-formed[.](#5.sentence-3)

[6](#6)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L4435)

All access controls in [[class.access]](class.access "11.8 Member access control") affect the ability to name a class member
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 [*member-specification*](class.mem.general#nt:member-specification "11.4.1 General [class.mem.general]")[.](#6.sentence-1)

[*Note [5](#note-5)*:

This access also applies to implicit references to constructors,
conversion functions, and destructors[.](#6.sentence-2)

â *end note*]

[7](#7)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L4446)

[*Example [4](#example-4)*: class A {typedef int I; // private member I f() pre(A::x > 0); friend I g(I) post(A::x <= 0); static I x; template<int> struct Q; template<int> friend struct R;protected:struct B { };};

A::I A::f() pre(A::x > 0) { return 0; } A::I g(A::I p = A::x) post(A::x <= 0);
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 { };

Here, all the uses ofA​::​I are well-formed becauseA​::​f,A​::​x, and A​::​Q are members of classA andg and R are friends of classA[.](#7.sentence-1)

This implies, for example, that access checking on the first use ofA​::​I must be deferred until it is determined that this use ofA​::​I is as the return type of a member of classA[.](#7.sentence-2)

Similarly, the use of A​::​B as a[*base-specifier*](class.derived.general#nt:base-specifier "11.7.1 General [class.derived.general]") is well-formed because D is derived from A, so checking of [*base-specifier*](class.derived.general#nt:base-specifier "11.7.1 General [class.derived.general]")*s* must be deferred until the entire [*base-specifier-list*](class.derived.general#nt:base-specifier-list "11.7.1 General [class.derived.general]") has been seen[.](#7.sentence-3)

â *end example*]

[8](#8)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L4493)

Access is checked for a default argument ([[dcl.fct.default]](dcl.fct.default "9.3.4.7 Default arguments"))
at the point of declaration,
rather than at any points of use of the default argument[.](#8.sentence-1)

Access checking for default arguments in function templates and in
member functions of class templates is performed as described in [[temp.inst]](temp.inst "13.9.2 Implicit instantiation")[.](#8.sentence-2)

[9](#9)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L4502)

Access for a default [*template-argument*](temp.names#nt:template-argument "13.3 Names of template specializations [temp.names]") ([[temp.param]](temp.param "13.2 Template parameters"))
is checked in the context in which it appears rather than at any
points of use of it[.](#9.sentence-1)

[*Example [5](#example-5)*: 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 example*]

[96)](#footnote-96)[96)](#footnoteref-96)

Access
permissions are thus transitive and cumulative to nested
and local classes[.](#footnote-96.sentence-1)
