tmp/tmp5i_owxl5/{from.md → to.md}
RENAMED
|
@@ -1,18 +1,15 @@
|
|
| 1 |
#### The `this` pointer <a id="class.this">[[class.this]]</a>
|
| 2 |
|
| 3 |
-
In the body of a non-static
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
`
|
| 8 |
-
declared `volatile`, the type of `this` is `volatile` `X*`, and if the
|
| 9 |
-
member function is declared `const` `volatile`, the type of `this` is
|
| 10 |
-
`const` `volatile` `X*`.
|
| 11 |
|
| 12 |
-
[*Note 1*: Thus in a
|
| 13 |
-
function is called is accessed through a
|
| 14 |
path. — *end note*]
|
| 15 |
|
| 16 |
[*Example 1*:
|
| 17 |
|
| 18 |
``` cpp
|
|
@@ -26,22 +23,23 @@ struct s {
|
|
| 26 |
int s::f() const { return a; }
|
| 27 |
```
|
| 28 |
|
| 29 |
The `a++` in the body of `s::h` is ill-formed because it tries to modify
|
| 30 |
(a part of) the object for which `s::h()` is called. This is not allowed
|
| 31 |
-
in a
|
| 32 |
-
|
| 33 |
|
| 34 |
— *end example*]
|
| 35 |
|
| 36 |
-
Similarly, `volatile` semantics
|
| 37 |
-
member functions when accessing the object and its non-static
|
| 38 |
-
members.
|
| 39 |
|
| 40 |
-
A
|
| 41 |
-
[[expr.ref]]
|
| 42 |
-
|
|
|
|
| 43 |
|
| 44 |
[*Example 2*:
|
| 45 |
|
| 46 |
``` cpp
|
| 47 |
void k(s& x, const s& y) {
|
|
@@ -51,17 +49,15 @@ void k(s& x, const s& y) {
|
|
| 51 |
y.g(); // error
|
| 52 |
}
|
| 53 |
```
|
| 54 |
|
| 55 |
The call `y.g()` is ill-formed because `y` is `const` and `s::g()` is a
|
| 56 |
-
non-
|
| 57 |
-
|
| 58 |
|
| 59 |
— *end example*]
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
[
|
| 65 |
-
destroy objects with cv-qualified types, see [[class.ctor]] and
|
| 66 |
-
[[class.dtor]]. — *end note*]
|
| 67 |
|
|
|
|
| 1 |
#### The `this` pointer <a id="class.this">[[class.this]]</a>
|
| 2 |
|
| 3 |
+
In the body of a non-static [[class.mfct]] member function, the keyword
|
| 4 |
+
`this` is a prvalue whose value is a pointer to the object for which the
|
| 5 |
+
function is called. The type of `this` in a member function whose type
|
| 6 |
+
has a *cv-qualifier-seq* cv and whose class is `X` is “pointer to cv
|
| 7 |
+
`X`”.
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
[*Note 1*: Thus in a const member function, the object for which the
|
| 10 |
+
function is called is accessed through a const access
|
| 11 |
path. — *end note*]
|
| 12 |
|
| 13 |
[*Example 1*:
|
| 14 |
|
| 15 |
``` cpp
|
|
|
|
| 23 |
int s::f() const { return a; }
|
| 24 |
```
|
| 25 |
|
| 26 |
The `a++` in the body of `s::h` is ill-formed because it tries to modify
|
| 27 |
(a part of) the object for which `s::h()` is called. This is not allowed
|
| 28 |
+
in a const member function because `this` is a pointer to `const`; that
|
| 29 |
+
is, `*this` has `const` type.
|
| 30 |
|
| 31 |
— *end example*]
|
| 32 |
|
| 33 |
+
[*Note 2*: Similarly, `volatile` semantics [[dcl.type.cv]] apply in
|
| 34 |
+
volatile member functions when accessing the object and its non-static
|
| 35 |
+
data members. — *end note*]
|
| 36 |
|
| 37 |
+
A member function whose type has a *cv-qualifier-seq* *cv1* can be
|
| 38 |
+
called on an object expression [[expr.ref]] of type *cv2* `T` only if
|
| 39 |
+
*cv1* is the same as or more cv-qualified than *cv2*
|
| 40 |
+
[[basic.type.qualifier]].
|
| 41 |
|
| 42 |
[*Example 2*:
|
| 43 |
|
| 44 |
``` cpp
|
| 45 |
void k(s& x, const s& y) {
|
|
|
|
| 49 |
y.g(); // error
|
| 50 |
}
|
| 51 |
```
|
| 52 |
|
| 53 |
The call `y.g()` is ill-formed because `y` is `const` and `s::g()` is a
|
| 54 |
+
non-const member function, that is, `s::g()` is less-qualified than the
|
| 55 |
+
object expression `y`.
|
| 56 |
|
| 57 |
— *end example*]
|
| 58 |
|
| 59 |
+
[*Note 3*: Constructors and destructors cannot be declared `const`,
|
| 60 |
+
`volatile`, or `const` `volatile`. However, these functions can be
|
| 61 |
+
invoked to create and destroy objects with cv-qualified types; see
|
| 62 |
+
[[class.ctor]] and [[class.dtor]]. — *end note*]
|
|
|
|
|
|
|
| 63 |
|