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