Files
2025-10-25 03:02:53 +03:00

7.5 KiB
Raw Permalink Blame History

[class.access.general]

11 Classes [class]

11.8 Member access control [class.access]

11.8.1 General [class.access.general]

1

#

A member of a class can be

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

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]); or

public, that is, it can be named anywhere without access restriction.

[Note 1:

A constructor or destructor can be named by an expression ([basic.def.odr]) even though it has no name.

— end note]

2

#

A member of a class can also access all the members to which the class has access.

A local class of a member function may access the same members that the member function itself may access.96

3

#

Members of a class defined with the keywordclass are private by default.

Members of a class defined with the keywordsstruct or union are public by default.

[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

#

Access control is applied uniformly to declarations and expressions.

[Note 2:

Access control applies to members nominated by friend declarations ([class.friend]) andusing-declarations ([namespace.udecl]).

— end note]

When a using-declarator is named, access control is applied to it, not to the declarations that replace it.

For an overload set, access control is applied only to the function selected by overload resolution.

[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:

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.

The accessibility of the underlying entity is not considered.

[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

#

[Note 4:

Access control does not prevent members from being found by name lookup or implicit conversions to base classes from being considered.

— end note]

The interpretation of a given construct is established without regard to access control.

If the interpretation established makes use of inaccessible members or base classes, the construct is ill-formed.

6

#

All access controls in [class.access] 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.

[Note 5:

This access also applies to implicit references to constructors, conversion functions, and destructors.

— end note]

7

#

[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 struct Q; template 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.

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.

Similarly, the use of A::B as abase-specifier is well-formed because D is derived from A, so checking of base-specifiers must be deferred until the entire base-specifier-list has been seen.

— end example]

8

#

Access is checked for a default argument ([dcl.fct.default]) at the point of declaration, 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 [temp.inst].

9

#

Access for a default template-argument ([temp.param]) is checked in the context in which it appears rather than at any points of use of it.

[Example 5: class B { };template class C {protected:typedef T TT;};

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

D <C >* d; // access error, C::TT is protected — end example]

96)96)

Access permissions are thus transitive and cumulative to nested and local classes.