tmp/tmpkudfkumi/{from.md → to.md}
RENAMED
|
@@ -2,12 +2,15 @@
|
|
| 2 |
|
| 3 |
A friend of a class is a function or class that is given permission to
|
| 4 |
use the private and protected member names from the class. A class
|
| 5 |
specifies its friends, if any, by way of friend declarations. Such
|
| 6 |
declarations give special access rights to the friends, but they do not
|
| 7 |
-
make the nominated friends members of the befriending class.
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
| 9 |
friends:
|
| 10 |
|
| 11 |
``` cpp
|
| 12 |
class X {
|
| 13 |
int a;
|
|
@@ -24,14 +27,18 @@ void f() {
|
|
| 24 |
friend_set(&obj,10);
|
| 25 |
obj.member_set(10);
|
| 26 |
}
|
| 27 |
```
|
| 28 |
|
|
|
|
|
|
|
| 29 |
Declaring a class to be a friend implies that the names of private and
|
| 30 |
protected members from the class granting friendship can be accessed in
|
| 31 |
the *base-specifier*s and member declarations of the befriended class.
|
| 32 |
|
|
|
|
|
|
|
| 33 |
``` cpp
|
| 34 |
class A {
|
| 35 |
class B { };
|
| 36 |
friend class X;
|
| 37 |
};
|
|
@@ -42,10 +49,14 @@ struct X : A::B { // OK: A::B accessible to friend
|
|
| 42 |
A::B my; // OK: A::B accessible to nested member of friend
|
| 43 |
};
|
| 44 |
};
|
| 45 |
```
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
``` cpp
|
| 48 |
class X {
|
| 49 |
enum { a=100 };
|
| 50 |
friend class Y;
|
| 51 |
};
|
|
@@ -57,32 +68,42 @@ class Y {
|
|
| 57 |
class Z {
|
| 58 |
int v[X::a]; // error: X::a is private
|
| 59 |
};
|
| 60 |
```
|
| 61 |
|
|
|
|
|
|
|
| 62 |
A class shall not be defined in a friend declaration.
|
| 63 |
|
|
|
|
|
|
|
| 64 |
``` cpp
|
| 65 |
class A {
|
| 66 |
friend class B { }; // error: cannot define class in friend declaration
|
| 67 |
};
|
| 68 |
```
|
| 69 |
|
|
|
|
|
|
|
| 70 |
A `friend` declaration that does not declare a function shall have one
|
| 71 |
of the following forms:
|
| 72 |
|
| 73 |
``` bnf
|
| 74 |
'friend' elaborated-type-specifier ';'
|
| 75 |
'friend' simple-type-specifier ';'
|
| 76 |
'friend' typename-specifier ';'
|
| 77 |
```
|
| 78 |
|
| 79 |
-
A `friend` declaration may be the *declaration* in a
|
| 80 |
-
*template-declaration* (Clause [[temp]],
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
declaration
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
``` cpp
|
| 86 |
class C;
|
| 87 |
typedef C Ct;
|
| 88 |
|
|
@@ -102,41 +123,51 @@ template <typename T> class R {
|
|
| 102 |
|
| 103 |
R<C> rc; // class C is a friend of R<C>
|
| 104 |
R<int> Ri; // OK: "friend int;" is ignored
|
| 105 |
```
|
| 106 |
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
|
|
|
|
|
|
| 110 |
|
| 111 |
When a `friend` declaration refers to an overloaded name or operator,
|
| 112 |
only the function specified by the parameter types becomes a friend. A
|
| 113 |
member function of a class `X` can be a friend of a class `Y`.
|
| 114 |
|
|
|
|
|
|
|
| 115 |
``` cpp
|
| 116 |
class Y {
|
| 117 |
friend char* X::foo(int);
|
| 118 |
friend X::X(char); // constructors can be friends
|
| 119 |
friend X::~X(); // destructors can be friends
|
| 120 |
};
|
| 121 |
```
|
| 122 |
|
|
|
|
|
|
|
| 123 |
A function can be defined in a friend declaration of a class if and only
|
| 124 |
if the class is a non-local class ([[class.local]]), the function name
|
| 125 |
is unqualified, and the function has namespace scope.
|
| 126 |
|
|
|
|
|
|
|
| 127 |
``` cpp
|
| 128 |
class M {
|
| 129 |
friend void f() { } // definition of global f, a friend of M,
|
| 130 |
// not the definition of a member function
|
| 131 |
};
|
| 132 |
```
|
| 133 |
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
|
|
|
|
|
|
| 138 |
|
| 139 |
No *storage-class-specifier* shall appear in the *decl-specifier-seq* of
|
| 140 |
a friend declaration.
|
| 141 |
|
| 142 |
A name nominated by a friend declaration shall be accessible in the
|
|
@@ -145,10 +176,12 @@ friend declaration is the same whether the friend declaration appears in
|
|
| 145 |
the `private`, `protected` or `public` ([[class.mem]]) portion of the
|
| 146 |
class *member-specification*.
|
| 147 |
|
| 148 |
Friendship is neither inherited nor transitive.
|
| 149 |
|
|
|
|
|
|
|
| 150 |
``` cpp
|
| 151 |
class A {
|
| 152 |
friend class B;
|
| 153 |
int a;
|
| 154 |
};
|
|
@@ -157,33 +190,35 @@ class B {
|
|
| 157 |
friend class C;
|
| 158 |
};
|
| 159 |
|
| 160 |
class C {
|
| 161 |
void f(A* p) {
|
| 162 |
-
p->a++; // error: C is not a friend of A
|
| 163 |
-
// despite being a friend of a friend
|
| 164 |
}
|
| 165 |
};
|
| 166 |
|
| 167 |
class D : public B {
|
| 168 |
void f(A* p) {
|
| 169 |
-
p->a++; // error: D is not a friend of A
|
| 170 |
-
// despite being derived from a friend
|
| 171 |
}
|
| 172 |
};
|
| 173 |
```
|
| 174 |
|
|
|
|
|
|
|
| 175 |
If a friend declaration appears in a local class ([[class.local]]) and
|
| 176 |
the name specified is an unqualified name, a prior declaration is looked
|
| 177 |
up without considering scopes that are outside the innermost enclosing
|
| 178 |
non-class scope. For a friend function declaration, if there is no prior
|
| 179 |
declaration, the program is ill-formed. For a friend class declaration,
|
| 180 |
if there is no prior declaration, the class that is specified belongs to
|
| 181 |
the innermost enclosing non-class scope, but if it is subsequently
|
| 182 |
referenced, its name is not found by name lookup until a matching
|
| 183 |
declaration is provided in the innermost enclosing non-class scope.
|
| 184 |
|
|
|
|
|
|
|
| 185 |
``` cpp
|
| 186 |
class X;
|
| 187 |
void a();
|
| 188 |
void f() {
|
| 189 |
class Y;
|
|
@@ -199,5 +234,7 @@ void f() {
|
|
| 199 |
X* px; // OK, but ::X is found
|
| 200 |
Z* pz; // error, no Z is found
|
| 201 |
}
|
| 202 |
```
|
| 203 |
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
A friend of a class is a function or class that is given permission to
|
| 4 |
use the private and protected member names from the class. A class
|
| 5 |
specifies its friends, if any, by way of friend declarations. Such
|
| 6 |
declarations give special access rights to the friends, but they do not
|
| 7 |
+
make the nominated friends members of the befriending class.
|
| 8 |
+
|
| 9 |
+
[*Example 1*:
|
| 10 |
+
|
| 11 |
+
The following example illustrates the differences between members and
|
| 12 |
friends:
|
| 13 |
|
| 14 |
``` cpp
|
| 15 |
class X {
|
| 16 |
int a;
|
|
|
|
| 27 |
friend_set(&obj,10);
|
| 28 |
obj.member_set(10);
|
| 29 |
}
|
| 30 |
```
|
| 31 |
|
| 32 |
+
— *end example*]
|
| 33 |
+
|
| 34 |
Declaring a class to be a friend implies that the names of private and
|
| 35 |
protected members from the class granting friendship can be accessed in
|
| 36 |
the *base-specifier*s and member declarations of the befriended class.
|
| 37 |
|
| 38 |
+
[*Example 2*:
|
| 39 |
+
|
| 40 |
``` cpp
|
| 41 |
class A {
|
| 42 |
class B { };
|
| 43 |
friend class X;
|
| 44 |
};
|
|
|
|
| 49 |
A::B my; // OK: A::B accessible to nested member of friend
|
| 50 |
};
|
| 51 |
};
|
| 52 |
```
|
| 53 |
|
| 54 |
+
— *end example*]
|
| 55 |
+
|
| 56 |
+
[*Example 3*:
|
| 57 |
+
|
| 58 |
``` cpp
|
| 59 |
class X {
|
| 60 |
enum { a=100 };
|
| 61 |
friend class Y;
|
| 62 |
};
|
|
|
|
| 68 |
class Z {
|
| 69 |
int v[X::a]; // error: X::a is private
|
| 70 |
};
|
| 71 |
```
|
| 72 |
|
| 73 |
+
— *end example*]
|
| 74 |
+
|
| 75 |
A class shall not be defined in a friend declaration.
|
| 76 |
|
| 77 |
+
[*Example 4*:
|
| 78 |
+
|
| 79 |
``` cpp
|
| 80 |
class A {
|
| 81 |
friend class B { }; // error: cannot define class in friend declaration
|
| 82 |
};
|
| 83 |
```
|
| 84 |
|
| 85 |
+
— *end example*]
|
| 86 |
+
|
| 87 |
A `friend` declaration that does not declare a function shall have one
|
| 88 |
of the following forms:
|
| 89 |
|
| 90 |
``` bnf
|
| 91 |
'friend' elaborated-type-specifier ';'
|
| 92 |
'friend' simple-type-specifier ';'
|
| 93 |
'friend' typename-specifier ';'
|
| 94 |
```
|
| 95 |
|
| 96 |
+
[*Note 1*: A `friend` declaration may be the *declaration* in a
|
| 97 |
+
*template-declaration* (Clause [[temp]],
|
| 98 |
+
[[temp.friend]]). — *end note*]
|
| 99 |
+
|
| 100 |
+
If the type specifier in a `friend` declaration designates a (possibly
|
| 101 |
+
cv-qualified) class type, that class is declared as a friend; otherwise,
|
| 102 |
+
the `friend` declaration is ignored.
|
| 103 |
+
|
| 104 |
+
[*Example 5*:
|
| 105 |
|
| 106 |
``` cpp
|
| 107 |
class C;
|
| 108 |
typedef C Ct;
|
| 109 |
|
|
|
|
| 123 |
|
| 124 |
R<C> rc; // class C is a friend of R<C>
|
| 125 |
R<int> Ri; // OK: "friend int;" is ignored
|
| 126 |
```
|
| 127 |
|
| 128 |
+
— *end example*]
|
| 129 |
+
|
| 130 |
+
A function first declared in a friend declaration has the linkage of the
|
| 131 |
+
namespace of which it is a member ([[basic.link]]). Otherwise, the
|
| 132 |
+
function retains its previous linkage ([[dcl.stc]]).
|
| 133 |
|
| 134 |
When a `friend` declaration refers to an overloaded name or operator,
|
| 135 |
only the function specified by the parameter types becomes a friend. A
|
| 136 |
member function of a class `X` can be a friend of a class `Y`.
|
| 137 |
|
| 138 |
+
[*Example 6*:
|
| 139 |
+
|
| 140 |
``` cpp
|
| 141 |
class Y {
|
| 142 |
friend char* X::foo(int);
|
| 143 |
friend X::X(char); // constructors can be friends
|
| 144 |
friend X::~X(); // destructors can be friends
|
| 145 |
};
|
| 146 |
```
|
| 147 |
|
| 148 |
+
— *end example*]
|
| 149 |
+
|
| 150 |
A function can be defined in a friend declaration of a class if and only
|
| 151 |
if the class is a non-local class ([[class.local]]), the function name
|
| 152 |
is unqualified, and the function has namespace scope.
|
| 153 |
|
| 154 |
+
[*Example 7*:
|
| 155 |
+
|
| 156 |
``` cpp
|
| 157 |
class M {
|
| 158 |
friend void f() { } // definition of global f, a friend of M,
|
| 159 |
// not the definition of a member function
|
| 160 |
};
|
| 161 |
```
|
| 162 |
|
| 163 |
+
— *end example*]
|
| 164 |
+
|
| 165 |
+
Such a function is implicitly an inline function ([[dcl.inline]]). A
|
| 166 |
+
`friend` function defined in a class is in the (lexical) scope of the
|
| 167 |
+
class in which it is defined. A friend function defined outside the
|
| 168 |
+
class is not ([[basic.lookup.unqual]]).
|
| 169 |
|
| 170 |
No *storage-class-specifier* shall appear in the *decl-specifier-seq* of
|
| 171 |
a friend declaration.
|
| 172 |
|
| 173 |
A name nominated by a friend declaration shall be accessible in the
|
|
|
|
| 176 |
the `private`, `protected` or `public` ([[class.mem]]) portion of the
|
| 177 |
class *member-specification*.
|
| 178 |
|
| 179 |
Friendship is neither inherited nor transitive.
|
| 180 |
|
| 181 |
+
[*Example 8*:
|
| 182 |
+
|
| 183 |
``` cpp
|
| 184 |
class A {
|
| 185 |
friend class B;
|
| 186 |
int a;
|
| 187 |
};
|
|
|
|
| 190 |
friend class C;
|
| 191 |
};
|
| 192 |
|
| 193 |
class C {
|
| 194 |
void f(A* p) {
|
| 195 |
+
p->a++; // error: C is not a friend of A despite being a friend of a friend
|
|
|
|
| 196 |
}
|
| 197 |
};
|
| 198 |
|
| 199 |
class D : public B {
|
| 200 |
void f(A* p) {
|
| 201 |
+
p->a++; // error: D is not a friend of A despite being derived from a friend
|
|
|
|
| 202 |
}
|
| 203 |
};
|
| 204 |
```
|
| 205 |
|
| 206 |
+
— *end example*]
|
| 207 |
+
|
| 208 |
If a friend declaration appears in a local class ([[class.local]]) and
|
| 209 |
the name specified is an unqualified name, a prior declaration is looked
|
| 210 |
up without considering scopes that are outside the innermost enclosing
|
| 211 |
non-class scope. For a friend function declaration, if there is no prior
|
| 212 |
declaration, the program is ill-formed. For a friend class declaration,
|
| 213 |
if there is no prior declaration, the class that is specified belongs to
|
| 214 |
the innermost enclosing non-class scope, but if it is subsequently
|
| 215 |
referenced, its name is not found by name lookup until a matching
|
| 216 |
declaration is provided in the innermost enclosing non-class scope.
|
| 217 |
|
| 218 |
+
[*Example 9*:
|
| 219 |
+
|
| 220 |
``` cpp
|
| 221 |
class X;
|
| 222 |
void a();
|
| 223 |
void f() {
|
| 224 |
class Y;
|
|
|
|
| 234 |
X* px; // OK, but ::X is found
|
| 235 |
Z* pz; // error, no Z is found
|
| 236 |
}
|
| 237 |
```
|
| 238 |
|
| 239 |
+
— *end example*]
|
| 240 |
+
|