- tmp/tmp3kj17fg_/{from.md → to.md} +40 -176
tmp/tmp3kj17fg_/{from.md → to.md}
RENAMED
|
@@ -1,44 +1,43 @@
|
|
| 1 |
-
## Member functions <a id="class.mfct">[[class.mfct]]</a>
|
| 2 |
-
|
| 3 |
-
Functions declared in the definition of a class, excluding those
|
| 4 |
-
declared with a `friend` specifier ([[class.friend]]), are called
|
| 5 |
-
member functions of that class. A member function may be declared
|
| 6 |
-
`static` in which case it is a *static* member function of its class (
|
| 7 |
-
[[class.static]]); otherwise it is a *non-static* member function of its
|
| 8 |
-
class ([[class.mfct.non-static]], [[class.this]]).
|
| 9 |
|
| 10 |
A member function may be defined ([[dcl.fct.def]]) in its class
|
| 11 |
definition, in which case it is an *inline* member function (
|
| 12 |
-
[[dcl.
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
|
| 22 |
-
An
|
| 23 |
defined outside of its class definition provided either its declaration
|
| 24 |
in the class definition or its definition outside of the class
|
| 25 |
-
definition declares the function as `inline`
|
| 26 |
-
class in namespace scope have external linkage. Member functions of a
|
| 27 |
-
local class ([[class.local]]) have no linkage. See [[basic.link]].
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
If the definition of a member function is lexically outside its class
|
| 35 |
definition, the member function name shall be qualified by its class
|
| 36 |
-
name using the `::` operator.
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
``` cpp
|
| 42 |
struct X {
|
| 43 |
typedef int T;
|
| 44 |
static T count;
|
|
@@ -52,176 +51,41 @@ notation `X::f` specifies that the function `f` is a member of class `X`
|
|
| 52 |
and in the scope of class `X`. In the function definition, the parameter
|
| 53 |
type `T` refers to the typedef member `T` declared in class `X` and the
|
| 54 |
default argument `count` refers to the static data member `count`
|
| 55 |
declared in class `X`.
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
Previously declared member functions may be mentioned in `friend`
|
| 61 |
declarations.
|
| 62 |
|
| 63 |
Member functions of a local class shall be defined inline in their class
|
| 64 |
definition, if they are defined at all.
|
| 65 |
|
|
|
|
|
|
|
| 66 |
A member function can be declared (but not defined) using a typedef for
|
| 67 |
a function type. The resulting member function has exactly the same type
|
| 68 |
as it would have if the function declarator were provided explicitly,
|
| 69 |
see [[dcl.fct]]. For example,
|
| 70 |
|
| 71 |
``` cpp
|
| 72 |
-
typedef void fv(
|
| 73 |
-
typedef void fvc(
|
| 74 |
struct S {
|
| 75 |
-
fv memfunc1; // equivalent to: void memfunc1(
|
| 76 |
void memfunc2();
|
| 77 |
-
fvc memfunc3; // equivalent to: void memfunc3(
|
| 78 |
};
|
| 79 |
fv S::* pmfv1 = &S::memfunc1;
|
| 80 |
fv S::* pmfv2 = &S::memfunc2;
|
| 81 |
fvc S::* pmfv3 = &S::memfunc3;
|
| 82 |
```
|
| 83 |
|
| 84 |
Also see [[temp.arg]].
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
A *non-static* member function may be called for an object of its class
|
| 89 |
-
type, or for an object of a class derived (Clause [[class.derived]])
|
| 90 |
-
from its class type, using the class member access syntax (
|
| 91 |
-
[[expr.ref]], [[over.match.call]]). A non-static member function may
|
| 92 |
-
also be called directly using the function call syntax ([[expr.call]],
|
| 93 |
-
[[over.match.call]]) from within the body of a member function of its
|
| 94 |
-
class or of a class derived from its class.
|
| 95 |
-
|
| 96 |
-
If a non-static member function of a class `X` is called for an object
|
| 97 |
-
that is not of type `X`, or of a type derived from `X`, the behavior is
|
| 98 |
-
undefined.
|
| 99 |
-
|
| 100 |
-
When an *id-expression* ([[expr.prim]]) that is not part of a class
|
| 101 |
-
member access syntax ([[expr.ref]]) and not used to form a pointer to
|
| 102 |
-
member ([[expr.unary.op]]) is used in a member of class `X` in a
|
| 103 |
-
context where `this` can be used ([[expr.prim.general]]), if name
|
| 104 |
-
lookup ([[basic.lookup]]) resolves the name in the *id-expression* to a
|
| 105 |
-
non-static non-type member of some class `C`, and if either the
|
| 106 |
-
*id-expression* is potentially evaluated or `C` is `X` or a base class
|
| 107 |
-
of `X`, the *id-expression* is transformed into a class member access
|
| 108 |
-
expression ([[expr.ref]]) using `(*this)` ([[class.this]]) as the
|
| 109 |
-
*postfix-expression* to the left of the `.` operator. If `C` is not `X`
|
| 110 |
-
or a base class of `X`, the class member access expression is
|
| 111 |
-
ill-formed. Similarly during name lookup, when an *unqualified-id* (
|
| 112 |
-
[[expr.prim]]) used in the definition of a member function for class `X`
|
| 113 |
-
resolves to a `static` member, an enumerator or a nested type of class
|
| 114 |
-
`X` or of a base class of `X`, the *unqualified-id* is transformed into
|
| 115 |
-
a *qualified-id* ([[expr.prim]]) in which the *nested-name-specifier*
|
| 116 |
-
names the class of the member function.
|
| 117 |
-
|
| 118 |
-
``` cpp
|
| 119 |
-
struct tnode {
|
| 120 |
-
char tword[20];
|
| 121 |
-
int count;
|
| 122 |
-
tnode* left;
|
| 123 |
-
tnode* right;
|
| 124 |
-
void set(const char*, tnode* l, tnode* r);
|
| 125 |
-
};
|
| 126 |
-
|
| 127 |
-
void tnode::set(const char* w, tnode* l, tnode* r) {
|
| 128 |
-
count = strlen(w)+1;
|
| 129 |
-
if (sizeof(tword)<=count)
|
| 130 |
-
perror("tnode string too long");
|
| 131 |
-
strcpy(tword,w);
|
| 132 |
-
left = l;
|
| 133 |
-
right = r;
|
| 134 |
-
}
|
| 135 |
-
|
| 136 |
-
void f(tnode n1, tnode n2) {
|
| 137 |
-
n1.set("abc",&n2,0);
|
| 138 |
-
n2.set("def",0,0);
|
| 139 |
-
}
|
| 140 |
-
```
|
| 141 |
-
|
| 142 |
-
In the body of the member function `tnode::set`, the member names
|
| 143 |
-
`tword`, `count`, `left`, and `right` refer to members of the object for
|
| 144 |
-
which the function is called. Thus, in the call `n1.set("abc",&n2,0)`,
|
| 145 |
-
`tword` refers to `n1.tword`, and in the call `n2.set("def",0,0)`, it
|
| 146 |
-
refers to `n2.tword`. The functions `strlen`, `perror`, and `strcpy` are
|
| 147 |
-
not members of the class `tnode` and should be declared elsewhere.[^4]
|
| 148 |
-
|
| 149 |
-
A non-static member function may be declared `const`, `volatile`, or
|
| 150 |
-
`const` `volatile`. These *cv-qualifiers* affect the type of the `this`
|
| 151 |
-
pointer ([[class.this]]). They also affect the function type (
|
| 152 |
-
[[dcl.fct]]) of the member function; a member function declared `const`
|
| 153 |
-
is a *const* member function, a member function declared `volatile` is a
|
| 154 |
-
*volatile* member function and a member function declared `const`
|
| 155 |
-
`volatile` is a *const volatile* member function.
|
| 156 |
-
|
| 157 |
-
``` cpp
|
| 158 |
-
struct X {
|
| 159 |
-
void g() const;
|
| 160 |
-
void h() const volatile;
|
| 161 |
-
};
|
| 162 |
-
```
|
| 163 |
-
|
| 164 |
-
`X::g` is a `const` member function and `X::h` is a `const` `volatile`
|
| 165 |
-
member function.
|
| 166 |
-
|
| 167 |
-
A non-static member function may be declared with a *ref-qualifier* (
|
| 168 |
-
[[dcl.fct]]); see [[over.match.funcs]].
|
| 169 |
-
|
| 170 |
-
A non-static member function may be declared *virtual* (
|
| 171 |
-
[[class.virtual]]) or *pure virtual* ([[class.abstract]]).
|
| 172 |
-
|
| 173 |
-
### The `this` pointer <a id="class.this">[[class.this]]</a>
|
| 174 |
-
|
| 175 |
-
In the body of a non-static ([[class.mfct]]) member function, the
|
| 176 |
-
keyword `this` is a prvalue expression whose value is the address of the
|
| 177 |
-
object for which the function is called. The type of `this` in a member
|
| 178 |
-
function of a class `X` is `X*`. If the member function is declared
|
| 179 |
-
`const`, the type of `this` is `const` `X*`, if the member function is
|
| 180 |
-
declared `volatile`, the type of `this` is `volatile` `X*`, and if the
|
| 181 |
-
member function is declared `const` `volatile`, the type of `this` is
|
| 182 |
-
`const` `volatile` `X*`. thus in a `const` member function, the object
|
| 183 |
-
for which the function is called is accessed through a `const` access
|
| 184 |
-
path.
|
| 185 |
-
|
| 186 |
-
``` cpp
|
| 187 |
-
struct s {
|
| 188 |
-
int a;
|
| 189 |
-
int f() const;
|
| 190 |
-
int g() { return a++; }
|
| 191 |
-
int h() const { return a++; } // error
|
| 192 |
-
};
|
| 193 |
-
|
| 194 |
-
int s::f() const { return a; }
|
| 195 |
-
```
|
| 196 |
-
|
| 197 |
-
The `a++` in the body of `s::h` is ill-formed because it tries to modify
|
| 198 |
-
(a part of) the object for which `s::h()` is called. This is not allowed
|
| 199 |
-
in a `const` member function because `this` is a pointer to `const`;
|
| 200 |
-
that is, `*this` has `const` type.
|
| 201 |
-
|
| 202 |
-
Similarly, `volatile` semantics ([[dcl.type.cv]]) apply in `volatile`
|
| 203 |
-
member functions when accessing the object and its non-static data
|
| 204 |
-
members.
|
| 205 |
-
|
| 206 |
-
A *cv-qualified* member function can be called on an object-expression (
|
| 207 |
-
[[expr.ref]]) only if the object-expression is as cv-qualified or
|
| 208 |
-
less-cv-qualified than the member function.
|
| 209 |
-
|
| 210 |
-
``` cpp
|
| 211 |
-
void k(s& x, const s& y) {
|
| 212 |
-
x.f();
|
| 213 |
-
x.g();
|
| 214 |
-
y.f();
|
| 215 |
-
y.g(); // error
|
| 216 |
-
}
|
| 217 |
-
```
|
| 218 |
-
|
| 219 |
-
The call `y.g()` is ill-formed because `y` is `const` and `s::g()` is a
|
| 220 |
-
non-`const` member function, that is, `s::g()` is less-qualified than
|
| 221 |
-
the object-expression `y`.
|
| 222 |
-
|
| 223 |
-
Constructors ([[class.ctor]]) and destructors ([[class.dtor]]) shall
|
| 224 |
-
not be declared `const`, `volatile` or `const` `volatile`. However,
|
| 225 |
-
these functions can be invoked to create and destroy objects with
|
| 226 |
-
cv-qualified types, see ([[class.ctor]]) and ([[class.dtor]]).
|
| 227 |
|
|
|
|
| 1 |
+
### Member functions <a id="class.mfct">[[class.mfct]]</a>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
A member function may be defined ([[dcl.fct.def]]) in its class
|
| 4 |
definition, in which case it is an *inline* member function (
|
| 5 |
+
[[dcl.inline]]), or it may be defined outside of its class definition if
|
| 6 |
+
it has already been declared but not defined in its class definition. A
|
| 7 |
+
member function definition that appears outside of the class definition
|
| 8 |
+
shall appear in a namespace scope enclosing the class definition. Except
|
| 9 |
+
for member function definitions that appear outside of a class
|
| 10 |
+
definition, and except for explicit specializations of member functions
|
| 11 |
+
of class templates and member function templates ([[temp.spec]])
|
| 12 |
+
appearing outside of the class definition, a member function shall not
|
| 13 |
+
be redeclared.
|
| 14 |
|
| 15 |
+
An inline member function (whether static or non-static) may also be
|
| 16 |
defined outside of its class definition provided either its declaration
|
| 17 |
in the class definition or its definition outside of the class
|
| 18 |
+
definition declares the function as `inline` or `constexpr`.
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
[*Note 1*: Member functions of a class in namespace scope have the
|
| 21 |
+
linkage of that class. Member functions of a local class (
|
| 22 |
+
[[class.local]]) have no linkage. See [[basic.link]]. — *end note*]
|
| 23 |
+
|
| 24 |
+
[*Note 2*: There can be at most one definition of a non-inline member
|
| 25 |
+
function in a program. There may be more than one `inline` member
|
| 26 |
+
function definition in a program. See [[basic.def.odr]] and
|
| 27 |
+
[[dcl.inline]]. — *end note*]
|
| 28 |
|
| 29 |
If the definition of a member function is lexically outside its class
|
| 30 |
definition, the member function name shall be qualified by its class
|
| 31 |
+
name using the `::` operator.
|
| 32 |
+
|
| 33 |
+
[*Note 3*: A name used in a member function definition (that is, in the
|
| 34 |
+
*parameter-declaration-clause* including the default arguments (
|
| 35 |
+
[[dcl.fct.default]]) or in the member function body) is looked up as
|
| 36 |
+
described in [[basic.lookup]]. — *end note*]
|
| 37 |
+
|
| 38 |
+
[*Example 1*:
|
| 39 |
|
| 40 |
``` cpp
|
| 41 |
struct X {
|
| 42 |
typedef int T;
|
| 43 |
static T count;
|
|
|
|
| 51 |
and in the scope of class `X`. In the function definition, the parameter
|
| 52 |
type `T` refers to the typedef member `T` declared in class `X` and the
|
| 53 |
default argument `count` refers to the static data member `count`
|
| 54 |
declared in class `X`.
|
| 55 |
|
| 56 |
+
— *end example*]
|
| 57 |
+
|
| 58 |
+
[*Note 4*: A `static` local variable or local type in a member function
|
| 59 |
+
always refers to the same entity, whether or not the member function is
|
| 60 |
+
`inline`. — *end note*]
|
| 61 |
|
| 62 |
Previously declared member functions may be mentioned in `friend`
|
| 63 |
declarations.
|
| 64 |
|
| 65 |
Member functions of a local class shall be defined inline in their class
|
| 66 |
definition, if they are defined at all.
|
| 67 |
|
| 68 |
+
[*Note 5*:
|
| 69 |
+
|
| 70 |
A member function can be declared (but not defined) using a typedef for
|
| 71 |
a function type. The resulting member function has exactly the same type
|
| 72 |
as it would have if the function declarator were provided explicitly,
|
| 73 |
see [[dcl.fct]]. For example,
|
| 74 |
|
| 75 |
``` cpp
|
| 76 |
+
typedef void fv();
|
| 77 |
+
typedef void fvc() const;
|
| 78 |
struct S {
|
| 79 |
+
fv memfunc1; // equivalent to: void memfunc1();
|
| 80 |
void memfunc2();
|
| 81 |
+
fvc memfunc3; // equivalent to: void memfunc3() const;
|
| 82 |
};
|
| 83 |
fv S::* pmfv1 = &S::memfunc1;
|
| 84 |
fv S::* pmfv2 = &S::memfunc2;
|
| 85 |
fvc S::* pmfv3 = &S::memfunc3;
|
| 86 |
```
|
| 87 |
|
| 88 |
Also see [[temp.arg]].
|
| 89 |
|
| 90 |
+
— *end note*]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|