tmp/tmpjidaw8o4/{from.md → to.md}
RENAMED
|
@@ -1,40 +1,35 @@
|
|
| 1 |
### Non-static member functions <a id="class.mfct.non-static">[[class.mfct.non-static]]</a>
|
| 2 |
|
| 3 |
A non-static member function may be called for an object of its class
|
| 4 |
-
type, or for an object of a class derived
|
| 5 |
-
|
| 6 |
-
[[
|
| 7 |
-
|
| 8 |
-
[[over.match.call]]) from within
|
| 9 |
-
class or
|
| 10 |
|
| 11 |
If a non-static member function of a class `X` is called for an object
|
| 12 |
that is not of type `X`, or of a type derived from `X`, the behavior is
|
| 13 |
undefined.
|
| 14 |
|
| 15 |
-
When an *id-expression*
|
| 16 |
-
member access syntax
|
| 17 |
-
member
|
| 18 |
-
|
| 19 |
-
[[basic.lookup]]
|
| 20 |
non-static non-type member of some class `C`, and if either the
|
| 21 |
*id-expression* is potentially evaluated or `C` is `X` or a base class
|
| 22 |
of `X`, the *id-expression* is transformed into a class member access
|
| 23 |
-
expression
|
| 24 |
*postfix-expression* to the left of the `.` operator.
|
| 25 |
|
| 26 |
[*Note 1*: If `C` is not `X` or a base class of `X`, the class member
|
| 27 |
access expression is ill-formed. — *end note*]
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
static member, an enumerator or a nested type of class `X` or of a base
|
| 32 |
-
class of `X`, the *unqualified-id* is transformed into a
|
| 33 |
-
*qualified-id* ([[expr.prim]]) in which the *nested-name-specifier*
|
| 34 |
-
names the class of the member function. These transformations do not
|
| 35 |
-
apply in the template definition context ([[temp.dep.type]]).
|
| 36 |
|
| 37 |
[*Example 1*:
|
| 38 |
|
| 39 |
``` cpp
|
| 40 |
struct tnode {
|
|
@@ -63,55 +58,52 @@ void f(tnode n1, tnode n2) {
|
|
| 63 |
In the body of the member function `tnode::set`, the member names
|
| 64 |
`tword`, `count`, `left`, and `right` refer to members of the object for
|
| 65 |
which the function is called. Thus, in the call `n1.set("abc",&n2,0)`,
|
| 66 |
`tword` refers to `n1.tword`, and in the call `n2.set("def",0,0)`, it
|
| 67 |
refers to `n2.tword`. The functions `strlen`, `perror`, and `strcpy` are
|
| 68 |
-
not members of the class `tnode` and should be declared elsewhere.[^
|
| 69 |
|
| 70 |
— *end example*]
|
| 71 |
|
| 72 |
A non-static member function may be declared `const`, `volatile`, or
|
| 73 |
`const` `volatile`. These *cv-qualifier*s affect the type of the `this`
|
| 74 |
-
pointer
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
|
| 80 |
[*Example 2*:
|
| 81 |
|
| 82 |
``` cpp
|
| 83 |
struct X {
|
| 84 |
void g() const;
|
| 85 |
void h() const volatile;
|
| 86 |
};
|
| 87 |
```
|
| 88 |
|
| 89 |
-
`X::g` is a
|
| 90 |
-
|
| 91 |
|
| 92 |
— *end example*]
|
| 93 |
|
| 94 |
-
A non-static member function may be declared with a *ref-qualifier*
|
| 95 |
-
[[dcl.fct]]
|
| 96 |
|
| 97 |
-
A non-static member function may be declared
|
| 98 |
-
|
| 99 |
|
| 100 |
#### The `this` pointer <a id="class.this">[[class.this]]</a>
|
| 101 |
|
| 102 |
-
In the body of a non-static
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
`
|
| 107 |
-
declared `volatile`, the type of `this` is `volatile` `X*`, and if the
|
| 108 |
-
member function is declared `const` `volatile`, the type of `this` is
|
| 109 |
-
`const` `volatile` `X*`.
|
| 110 |
|
| 111 |
-
[*Note 1*: Thus in a
|
| 112 |
-
function is called is accessed through a
|
| 113 |
path. — *end note*]
|
| 114 |
|
| 115 |
[*Example 1*:
|
| 116 |
|
| 117 |
``` cpp
|
|
@@ -125,22 +117,23 @@ struct s {
|
|
| 125 |
int s::f() const { return a; }
|
| 126 |
```
|
| 127 |
|
| 128 |
The `a++` in the body of `s::h` is ill-formed because it tries to modify
|
| 129 |
(a part of) the object for which `s::h()` is called. This is not allowed
|
| 130 |
-
in a
|
| 131 |
-
|
| 132 |
|
| 133 |
— *end example*]
|
| 134 |
|
| 135 |
-
Similarly, `volatile` semantics
|
| 136 |
-
member functions when accessing the object and its non-static
|
| 137 |
-
members.
|
| 138 |
|
| 139 |
-
A
|
| 140 |
-
[[expr.ref]]
|
| 141 |
-
|
|
|
|
| 142 |
|
| 143 |
[*Example 2*:
|
| 144 |
|
| 145 |
``` cpp
|
| 146 |
void k(s& x, const s& y) {
|
|
@@ -150,17 +143,15 @@ void k(s& x, const s& y) {
|
|
| 150 |
y.g(); // error
|
| 151 |
}
|
| 152 |
```
|
| 153 |
|
| 154 |
The call `y.g()` is ill-formed because `y` is `const` and `s::g()` is a
|
| 155 |
-
non-
|
| 156 |
-
|
| 157 |
|
| 158 |
— *end example*]
|
| 159 |
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
[
|
| 164 |
-
destroy objects with cv-qualified types, see [[class.ctor]] and
|
| 165 |
-
[[class.dtor]]. — *end note*]
|
| 166 |
|
|
|
|
| 1 |
### Non-static member functions <a id="class.mfct.non-static">[[class.mfct.non-static]]</a>
|
| 2 |
|
| 3 |
A non-static member function may be called for an object of its class
|
| 4 |
+
type, or for an object of a class derived [[class.derived]] from its
|
| 5 |
+
class type, using the class member access syntax ([[expr.ref]],
|
| 6 |
+
[[over.match.call]]). A non-static member function may also be called
|
| 7 |
+
directly using the function call syntax ([[expr.call]],
|
| 8 |
+
[[over.match.call]]) from within its class or a class derived from its
|
| 9 |
+
class, or a member thereof, as described below.
|
| 10 |
|
| 11 |
If a non-static member function of a class `X` is called for an object
|
| 12 |
that is not of type `X`, or of a type derived from `X`, the behavior is
|
| 13 |
undefined.
|
| 14 |
|
| 15 |
+
When an *id-expression* [[expr.prim.id]] that is not part of a class
|
| 16 |
+
member access syntax [[expr.ref]] and not used to form a pointer to
|
| 17 |
+
member [[expr.unary.op]] is used in a member of class `X` in a context
|
| 18 |
+
where `this` can be used [[expr.prim.this]], if name lookup
|
| 19 |
+
[[basic.lookup]] resolves the name in the *id-expression* to a
|
| 20 |
non-static non-type member of some class `C`, and if either the
|
| 21 |
*id-expression* is potentially evaluated or `C` is `X` or a base class
|
| 22 |
of `X`, the *id-expression* is transformed into a class member access
|
| 23 |
+
expression [[expr.ref]] using `(*this)` [[class.this]] as the
|
| 24 |
*postfix-expression* to the left of the `.` operator.
|
| 25 |
|
| 26 |
[*Note 1*: If `C` is not `X` or a base class of `X`, the class member
|
| 27 |
access expression is ill-formed. — *end note*]
|
| 28 |
|
| 29 |
+
This transformation does not apply in the template definition context
|
| 30 |
+
[[temp.dep.type]].
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
[*Example 1*:
|
| 33 |
|
| 34 |
``` cpp
|
| 35 |
struct tnode {
|
|
|
|
| 58 |
In the body of the member function `tnode::set`, the member names
|
| 59 |
`tword`, `count`, `left`, and `right` refer to members of the object for
|
| 60 |
which the function is called. Thus, in the call `n1.set("abc",&n2,0)`,
|
| 61 |
`tword` refers to `n1.tword`, and in the call `n2.set("def",0,0)`, it
|
| 62 |
refers to `n2.tword`. The functions `strlen`, `perror`, and `strcpy` are
|
| 63 |
+
not members of the class `tnode` and should be declared elsewhere.[^2]
|
| 64 |
|
| 65 |
— *end example*]
|
| 66 |
|
| 67 |
A non-static member function may be declared `const`, `volatile`, or
|
| 68 |
`const` `volatile`. These *cv-qualifier*s affect the type of the `this`
|
| 69 |
+
pointer [[class.this]]. They also affect the function type [[dcl.fct]]
|
| 70 |
+
of the member function; a member function declared `const` is a *const
|
| 71 |
+
member function*, a member function declared `volatile` is a *volatile
|
| 72 |
+
member function* and a member function declared `const` `volatile` is a
|
| 73 |
+
*const volatile member function*.
|
| 74 |
|
| 75 |
[*Example 2*:
|
| 76 |
|
| 77 |
``` cpp
|
| 78 |
struct X {
|
| 79 |
void g() const;
|
| 80 |
void h() const volatile;
|
| 81 |
};
|
| 82 |
```
|
| 83 |
|
| 84 |
+
`X::g` is a const member function and `X::h` is a const volatile member
|
| 85 |
+
function.
|
| 86 |
|
| 87 |
— *end example*]
|
| 88 |
|
| 89 |
+
A non-static member function may be declared with a *ref-qualifier*
|
| 90 |
+
[[dcl.fct]]; see [[over.match.funcs]].
|
| 91 |
|
| 92 |
+
A non-static member function may be declared virtual [[class.virtual]]
|
| 93 |
+
or pure virtual [[class.abstract]].
|
| 94 |
|
| 95 |
#### The `this` pointer <a id="class.this">[[class.this]]</a>
|
| 96 |
|
| 97 |
+
In the body of a non-static [[class.mfct]] member function, the keyword
|
| 98 |
+
`this` is a prvalue whose value is a pointer to the object for which the
|
| 99 |
+
function is called. The type of `this` in a member function whose type
|
| 100 |
+
has a *cv-qualifier-seq* cv and whose class is `X` is “pointer to cv
|
| 101 |
+
`X`”.
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
+
[*Note 1*: Thus in a const member function, the object for which the
|
| 104 |
+
function is called is accessed through a const access
|
| 105 |
path. — *end note*]
|
| 106 |
|
| 107 |
[*Example 1*:
|
| 108 |
|
| 109 |
``` cpp
|
|
|
|
| 117 |
int s::f() const { return a; }
|
| 118 |
```
|
| 119 |
|
| 120 |
The `a++` in the body of `s::h` is ill-formed because it tries to modify
|
| 121 |
(a part of) the object for which `s::h()` is called. This is not allowed
|
| 122 |
+
in a const member function because `this` is a pointer to `const`; that
|
| 123 |
+
is, `*this` has `const` type.
|
| 124 |
|
| 125 |
— *end example*]
|
| 126 |
|
| 127 |
+
[*Note 2*: Similarly, `volatile` semantics [[dcl.type.cv]] apply in
|
| 128 |
+
volatile member functions when accessing the object and its non-static
|
| 129 |
+
data members. — *end note*]
|
| 130 |
|
| 131 |
+
A member function whose type has a *cv-qualifier-seq* *cv1* can be
|
| 132 |
+
called on an object expression [[expr.ref]] of type *cv2* `T` only if
|
| 133 |
+
*cv1* is the same as or more cv-qualified than *cv2*
|
| 134 |
+
[[basic.type.qualifier]].
|
| 135 |
|
| 136 |
[*Example 2*:
|
| 137 |
|
| 138 |
``` cpp
|
| 139 |
void k(s& x, const s& y) {
|
|
|
|
| 143 |
y.g(); // error
|
| 144 |
}
|
| 145 |
```
|
| 146 |
|
| 147 |
The call `y.g()` is ill-formed because `y` is `const` and `s::g()` is a
|
| 148 |
+
non-const member function, that is, `s::g()` is less-qualified than the
|
| 149 |
+
object expression `y`.
|
| 150 |
|
| 151 |
— *end example*]
|
| 152 |
|
| 153 |
+
[*Note 3*: Constructors and destructors cannot be declared `const`,
|
| 154 |
+
`volatile`, or `const` `volatile`. However, these functions can be
|
| 155 |
+
invoked to create and destroy objects with cv-qualified types; see
|
| 156 |
+
[[class.ctor]] and [[class.dtor]]. — *end note*]
|
|
|
|
|
|
|
| 157 |
|