forked from cplusplus/draft
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccess.tex
More file actions
1018 lines (913 loc) · 26.5 KB
/
access.tex
File metadata and controls
1018 lines (913 loc) · 26.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%!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{ncsimplebnf}
access-specifier \terminal{:} member-specification\opt
\end{ncsimplebnf}
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 the linkage of the namespace of which it is a member~(\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 {