[class.access] (original) (raw)
11 Classes [class]
11.9 Member access control [class.access]
A member of a class can be
- private; that is, its name can be used only by members and friends of the class in which it is declared.
- 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 [class.protected]).
- public; that is, its name can be used anywhere without access restriction.
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.111
Members of a class defined with the keywordclassareprivateby default.
Members of a class defined with the keywordsstruct or unionare public by default.
[ Example
:
class X {
int a;
};
struct S {
int a;
};
— end example
]
Access control is applied uniformly to all names, whether the names are referred to from declarations or expressions.
[ Note
:
Access control applies to names nominated by friend declarations ([class.friend]) andusing-declarations.
— end note
]
In the case of overloaded function names, access control is applied to the function selected by overload resolution.
[ Note
:
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,
class A { class B { }; public: typedef B BB; };
void f() {
A::BB x;
A::B y;
}
— end note
]
[ Note
:
Access to members and base classes is controlled, not their visibility ([basic.scope.hiding]).
Names of members are still visible, and implicit conversions to base classes are still considered, when those members and base classes are inaccessible.
— end note
]
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.
All access controls in [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 member-specification.
[ Note
: This access also applies to implicit references to constructors, conversion functions, and destructors. — end note
]
[ Example
:
class A {
typedef int I;
I f();
friend I g(I);
static I x;
template struct Q;
template 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 { };
Here, all the uses ofA::Iare well-formed becauseA::f,A::x, and A::Qare members of classAandgand R are friends of classA.
This implies, for example, that access checking on the first use ofA::Imust be deferred until it is determined that this use ofA::Iis as the return type of a member of classA.
Similarly, the use of A::B as abase-specifier is well-formed because Dis derived from A, so checking of base-specifiersmust be deferred until the entire base-specifier-list has been seen.
— end example
]
The names in a default argument ([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 [temp.inst].
[ Example
:
class B { }; template class C { protected: typedef T TT; };
template <class U, class V = typename U::TT> class D : public U { };
D <C >* d;
— end example
]
11.9.1 Access specifiers [class.access.spec]
Any number of access specifiers is allowed and no particular order is required.
[ Example
:
struct S {
int a;
protected:
int b;
private:
int c;
public:
int d;
};
— end example
]
[ Note
:
The effect of access control on the order of allocation of data members is specified in [expr.rel].
— end note
]
When a member is redeclared within its class definition, the access specified at its redeclaration shall be the same as at its initial declaration.
[ Example
:
struct S {
class A;
enum E : int;
private:
class A { };
enum E: int { e0 };
};
— end example
]
[ Note
:
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.
— end note
]
[ Example
:
class A { };
class B : private A { };
class C : public B {
A* p;
::A* q;
};
— end example
]
11.9.2 Accessibility of base classes and base class members [class.access.base]
If a class is declared to be a base class ([class.derived]) for another class using thepublicaccess specifier, the public members of the base class are accessible as public members of the derived class and protected members of the base class are accessible as protected members of the derived class.
If a class is declared to be a base class for another class using theprotectedaccess specifier, the public and protected members of the base class are accessible as protected members of the derived class.
If a class is declared to be a base class for another class using theprivateaccess specifier, the public and protected members of the base class are accessible as private members of the derived class.112
In the absence of anaccess-specifierfor a base class,publicis assumed when the derived class is defined with the class-key structandprivateis assumed when the class is defined with the class-key class.
[ Example
:
class B { };
class D1 : private B { };
class D2 : public B { };
class D3 : B { };
struct D4 : public B { };
struct D5 : private B { };
struct D6 : B { };
class D7 : protected B { };
struct D8 : protected B { };
HereBis a public base ofD2,D4, andD6, a private base ofD1,D3, andD5, and a protected base ofD7andD8.
— end example
]
[ Note
:
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 ([conv.ptr]) and explicit casts ([expr.type.conv], [expr.static.cast], [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,
class B {
public:
int mi;
static int si;
};
class D : private B {
};
class DD : public D {
void f();
};
void DD::f() {
mi = 3;
si = 3;
::B b;
b.mi = 3;
b.si = 3;
::B::si = 3;
::B* bp1 = this;
::B* bp2 = (::B*)this;
bp2->mi = 3;
}
— end note
]
A base classBofNisaccessibleatR, if
- an invented public member ofBwould be a public member ofN, or
- Roccurs in a member or friend of classN, and an invented public member ofBwould be a private or protected member ofN, or
- Roccurs in a member or friend of a classPderived fromN, and an invented public member ofBwould be a private or protected member ofP, or
- there exists a classSsuch thatBis a base class ofSaccessible atRandSis a base class ofNaccessible atR.
[ Example
:
class B { public: int m; };
class S: private B { friend class N; };
class N: private S { void f() { B* p = this;
} };
— end example
]
If a base class is accessible, one can implicitly convert a pointer to a derived class to a pointer to that base class ([conv.ptr], [conv.mem]).
[ Note
:
It follows that members and friends of a classXcan implicitly convert anX*to a pointer to a private or protected immediate base class ofX.
— end note
]
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.
[ Note
:
This class can be explicit, e.g., when aqualified-idis used, or implicit, e.g., when a class member access operator ([expr.ref]) is used (including cases where an implicit “this->” is added).
If both a class member access operator and aqualified-idare used to name the member (as inp->T::m), the class naming the member is the class denoted by thenested-name-specifierof thequalified-id(that is,T).
— end note
]
A membermis accessible at the pointRwhen named in classNif
- mas a member ofNis public, or
- mas a member ofNis private, andRoccurs in a member or friend of classN, or
- mas a member ofNis protected, andRoccurs in a member or friend of classN, or in a member of a classPderived fromN, wheremas a member ofPis public, private, or protected, or
- there exists a base classBofNthat is accessible atR, andmis accessible atRwhen named in classB.
[ Example
:
class B;
class A {
private:
int i;
friend void f(B*);
};
class B : public A { };
void f(B* p) {
p->i = 1;
}
— end example
]
If a class member access operator, including an implicit “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 “.” operator case) cannot be implicitly converted to a pointer to the naming class of the right operand.
[ Note
:
This requirement is in addition to the requirement that the member be accessible as named.
— end note
]
11.9.3 Friends [class.friend]
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.
[ Example
:
The following example illustrates the differences between members and friends:
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 example
]
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 thebase-specifiers and member declarations of the befriended class.
[ Example
:
class A { class B { }; friend class X; };
struct X : A::B {
A::B mx;
class Y {
A::B my;
};
};
— end example
]
[ Example
:
class X { enum { a=100 }; friend class Y; };
class Y {
int v[X::a];
};
class Z {
int v[X::a];
};
— end example
]
A class shall not be defined in a friend declaration.
[ Example
:
class A {
friend class B { };
};
— end example
]
A friend declaration that does not declare a function shall have one of the following forms:
friend elaborated-type-specifier ; friend simple-type-specifier ; friend typename-specifier ;
If the type specifier in a friend declaration designates a (possibly cv-qualified) class type, that class is declared as a friend; otherwise, the friend declaration is ignored.
[ Example
:
class C; typedef C Ct;
class X1 {
friend C;
};
class X2 {
friend Ct;
friend D;
friend class D;
};
template class R { friend T; };
R rc;
R Ri;
— end example
]
A function first declared in a friend declaration has the linkage of the namespace of which it is a member ([basic.link],[namespace.memdef]).
Otherwise, the function retains its previous linkage ([dcl.stc]).
When a 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 classXcan be a friend of a classY.
[ Example
:
class Y {
friend char* X::foo(int);
friend X::X(char);
friend X::~X();
};
— end example
]
A function can be defined in a friend declaration of a class if and only if the class is a non-local class ([class.local]), the function name is unqualified, and the function has namespace scope.
[ Example
:
class M { friend void f() { }
};
— end example
]
Such a function is implicitly an inline ([dcl.inline]) function if it is attached to the global module.
A friend function defined in a class is in the (lexical) scope of the class in which it is defined.
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 private, protected, or public ([class.mem]) portion of the classmember-specification.
Friendship is neither inherited nor transitive.
[ Example
:
class A { friend class B; int a; };
class B { friend class C; };
class C {
void f(A* p) {
p->a++;
}
};
class D : public B {
void f(A* p) {
p->a++;
}
};
— end example
]
If a friend declaration appears in a local class ([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.
[ Example
:
class X;
void a();
void f() {
class Y;
extern void b();
class A {
friend class X;
friend class Y;
friend class Z;
friend void a();
friend void b();
friend void c();
};
X* px;
Z* pz;
}
— end example
]
11.9.4 Protected member access [class.protected]
An additional access check beyond those described earlier in [class.access]is applied when a non-static data member or non-static member function is a protected member of its naming class ([class.access.base]).113
As described earlier, access to a protected member is granted because the reference occurs in a friend or member of some class C.
All other accesses involve a (possibly implicit) object expression ([expr.ref]).
In this case, the class of the object expression shall beC or a class derived from C.
[ Example
:
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;
p1->i = 2;
p2->i = 3;
p2->B::i = 4;
int B::* pmi_B = &B::i;
int B::* pmi_B2 = &D2::i;
B::j = 5;
D2::j = 6;
}
void D2::mem(B* pb, D1* p1) {
pb->i = 1;
p1->i = 2;
i = 3;
B::i = 4;
int B::* pmi_B = &B::i;
int B::* pmi_B2 = &D2::i;
j = 5;
B::j = 6;
}
void g(B* pb, D1* p1, D2* p2) {
pb->i = 1;
p1->i = 2;
p2->i = 3;
}
— end example
]
11.9.5 Access to virtual functions [class.access.virt]
The access rules ([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.
[ Example
:
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();
pd->f();
}
— end example
]
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 (B*in the example above).
The access of the member function in the class in which it was defined (Din the example above) is in general not known.
11.9.6 Multiple access [class.paths]
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.
[ Example
:
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(); }
};
SinceW::f()is available toC::f()along the public path throughB, access is allowed.
— end example
]
11.9.7 Nested classes [class.access.nest]
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 ([class.access]) shall be obeyed.
[ Example
:
class E { int x; class B { };
class I {
B b;
int y;
void f(E* p, int i) {
p->x = i;
}
};
int g(I* p) {
return p->y;
}
};
— end example
]