Стандарт C++ 98 (1119566), страница 51
Текст из файла (страница 51)
[Example:struct S {class A;private:class A { };};// error: cannot change access—end example]11.2 Accessibility of base classes and base class members[class.access.base]1If a class is declared to be a base class (clause 10) for another class using the public access specifier, thepublic members of the base class are accessible as public members of the derived class andprotected members of the base class are accessible as protected members of the derived class. If aclass is declared to be a base class for another class using the protected access specifier, the publicand 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 the private access specifier, the publicand protected members of the base class are accessible as private members of the derived class99).2In the absence of an access-specifier for a base class, public is assumed when the derived class isdeclared struct and private is assumed when the class is declared class.
[Example:class B { /* ... */ };class D1 : private B { /* ... */ };class D2 : public B { /* ... */ };class D3 : B { /* ... */ };// B private by defaultstruct D4 : public B { /* ... */ };struct D5 : private B { /* ... */ };struct D6 : B { /* ... */ };// B public by defaultclass D7 : protected B { /* ... */ };struct D8 : protected B { /* ...
*/ };Here B is a public base of D2, D4, and D6, a private base of D1, D3, and D5, and a protected base of D7and D8. —end example]3[Note: A member of a private base class might be inaccessible as an inherited member name, but accessibledirectly. Because of the rules on pointer conversions (4.10) and explicit casts (5.4), a conversion from apointer 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();};// nonstatic member// static member__________________99) As specified previously in clause 11, private members of a base class remain inaccessible even to derived classes unless frienddeclarations within the base class declaration are used to grant access explicitly.177ISO/IEC 14882:1998(E)© ISO/IEC11.2 Accessibility of base classes and base class membersvoid 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;}11 Member access control// error: mi is private in D// error: si is private in D// OK (b.mi is different from this->mi)// OK (b.si is different from this->si)// OK// error: B is a private base class// OK with cast// OK: access through a pointer to B.—end note]4A base class is said to be accessible if an invented public member of the base class is accessible.
If a baseclass is accessible, one can implicitly convert a pointer to a derived class to a pointer to that base class(4.10, 4.11). [Note: it follows that members and friends of a class X can implicitly convert an X* to apointer to a private or protected immediate base class of X. ] The access to a member is affected by theclass in which the member is named.
This naming class is the class in which the member name was lookedup and found. [Note: this class can be explicit, e.g., when a qualified-id is used, or implicit, e.g., when aclass member access operator (5.2.5) is used (including cases where an implicit “this->” is added. Ifboth a class member access operator and a qualified-id are used to name the member (as in p->T::m), theclass naming the member is the class named by the nested-name-specifier of the qualified-id (that is, T). Ifthe member m is accessible when named in the naming class according to the rules below, the access to m isnonetheless ill-formed if the type of p cannot be implicitly converted to type T (for example, if T is an inaccessible base class of p’s class).
] A member m is accessible when named in class N if— m as a member of N is public, or— m as a member of N is private, and the reference occurs in a member or friend of class N, or— m as a member of N is protected, and the reference occurs in a member or friend of class N, or in amember or friend of a class P derived from N, where m as a member of P is private or protected, or— there exists a base class B of N that is accessible at the point of reference, and m is accessible whennamed in class B. [Example:class B;class A {private:int i;friend void f(B*);};class B : public A { };void f(B* p) {p->i = 1;// OK: B* can be implicitly cast to A*,// and f has access to i in A}—end example]11.3 Access declarations1[class.access.dcl]The access of a member of a base class can be changed in the derived class by mentioning its qualified-id inthe derived class declaration. Such mention is called an access declaration.
The effect of an access declaration qualified-id ; is defined to be equivalent to the declaration using qualified-id ;.100)__________________100) Access declarations are deprecated; member using-declarations (7.3.3) provide a better means of doing the same things. In earlierversions of the C++ language, access declarations were more limited; they were generalized and made equivalent to using-declarationsin the interest of simplicity.
Programmers are encouraged to use using-declarations, rather than the new capabilities of access declarations, in new code.178© ISO/IECISO/IEC 14882:1998(E)11 Member access control211.3 Access declarations[Example:class A {public:int z;int z1;};class B : public A {int a;public:int b, c;int bf();protected:int x;int y;};class D : private B {int d;public:B::c;B::z;A::z1;int e;int df();protected:B::x;int g;};// adjust access to B::c// adjust access to A::z// adjust access to A::z1// adjust access to B::xclass X : public D {int xf();};int ef(D&);int ff(X&);The external function ef can use only the names c, z, z1, e, and df.
Being a member of D, the functiondf can use the names b, c, z, z1, bf, x, y, d, e, df, and g, but not a. Being a member of B, the functionbf can use the members a, b, c, z, z1, bf, x, and y. The function xf can use the public and protectednames from D, that is, c, z, z1, e, and df (public), and x, and g (protected). Thus the external functionff has access only to c, z, z1, e, and df. If D were a protected or private base class of X, xf would havethe same privileges as before, but ff would have no access at all.
]11.4 Friends1[class.friend]A friend of a class is a function or class that is not a member of the class but is permitted to use the privateand protected member names from the class. The name of a friend is not in the scope of the class, and thefriend is not called with the member access operators (5.2.5) unless it is a member of another 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; }179ISO/IEC 14882:1998(E)© ISO/IEC11.4 Friends11 Member access controlvoid f(){X obj;friend_set(&obj,10);obj.member_set(10);}—end example]2Declaring a class to be a friend implies that the names of private and protected members from the classgranting friendship can be accessed in declarations of members of the befriended class. [Note: this meansthat access to private and protected names is also granted to member functions of the friend class (as if thefunctions were each friends) and to the static data member definitions of the friend class.
This also meansthat private and protected type names from the class granting friendship can be used in the base-clause of anested class of the friend class. However, the declarations of members of classes nested within the friendclass cannot access the names of private and protected members from the class granting friendship. Also,because the base-clause of the friend class is not part of its member declarations, the base-clause of thefriend class cannot access the names of the private and protected members from the class granting friendship. For example,class A {class B { };friend class X;};class X : A::B {A::B mx;class Y : A::B {A::B my;// ill-formed: A::B cannot be accessed// in the base-clause for X// OK: A::B used to declare member of X// OK: A::B used to declare member of X// ill-formed: A::B cannot be accessed// to declare members of nested class of X};};] An elaborated-type-specifier shall be used in a friend declaration for a class.101) A class shall not bedefined in a friend declaration.
[Example:class X {enum { a=100 };friend class Y;};class Y {int v[X::a];};// OK, Y is a friend of Xclass Z {int v[X::a];};// error: X::a is private—end example]3A function first declared in a friend declaration has external linkage (3.5). Otherwise, the function retainsits previous linkage (7.1.1).4When a friend declaration refers to an overloaded name or operator, only the function specified by theparameter types becomes a friend. A member function of a class X can be a friend of a class Y.