You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The **this** pointer is a pointer accessible only within the nonstatic member functions of a **class**, **struct**, or **union** type. It points to the object for which the member function is called. Static member functions do not have a **this** pointer.
12
+
The **this** pointer is a pointer accessible only within the nonstatic member functions of a **class**, **struct**, or **union** type. It points to the object for which the member function is called. Static member functions don't have a **this** pointer.
11
13
12
14
## Syntax
13
15
@@ -18,19 +20,19 @@ this->member-identifier
18
20
19
21
## Remarks
20
22
21
-
An object's **this** pointer is not part of the object itself; it is not reflected in the result of a **sizeof** statement on the object. Instead, when a nonstatic member function is called for an object, the address of the object is passed by the compiler as a hidden argument to the function. For example, the following function call:
23
+
An object's **this** pointer isn't part of the object itself. It's not reflected in the result of a **sizeof** statement on the object. When a nonstatic member function is called for an object, the compiler passes the object's address to the function as a hidden argument. For example, the following function call:
22
24
23
25
```cpp
24
26
myDate.setMonth( 3 );
25
27
```
26
28
27
-
can be interpreted this way:
29
+
can be interpreted as:
28
30
29
31
```cpp
30
32
setMonth( &myDate, 3 );
31
33
```
32
34
33
-
The object's address is available from within the member function as the **this** pointer. Most uses of **this** are implicit. It is legal, though unnecessary, to explicitly use **this** when referring to members of the class. For example:
35
+
The object's address is available from within the member function as the **this** pointer. Most **this** pointer uses are implicit. It's legal, though unnecessary, to use an explicit **this** when referring to members of the class. For example:
34
36
35
37
```cpp
36
38
void Date::setMonth( int mn )
@@ -55,7 +57,7 @@ if (&Object != this) {
55
57
```
56
58
57
59
> [!NOTE]
58
-
> Because the **this** pointer is nonmodifiable, assignments to **this** are not allowed. Earlier implementations of C++ allowed assignments to **this**.
60
+
> Because the **this** pointer is nonmodifiable, assignments to the **this** pointer are not allowed. Earlier implementations of C++ allowed assignment to **this**.
59
61
60
62
Occasionally, the **this** pointer is used directly — for example, to manipulate self-referential data structures, where the address of the current object is required.
61
63
@@ -131,9 +133,9 @@ your buffer
131
133
132
134
## Type of the this pointer
133
135
134
-
The **this** pointer's type can be modified in the function declaration by the **const** and **volatile** keywords. To declare a function as having the attributes of one or more of these keywords, add the keyword(s) after the function argument list.
136
+
The **this** pointer's type can be modified in the function declaration by the **const** and **volatile** keywords. To declare a function that has either of these attributes, add the keyword(s) after the function argument list.
135
137
136
-
Consider this example:
138
+
Consider an example:
137
139
138
140
```cpp
139
141
// type_of_this_pointer1.cpp
@@ -146,7 +148,7 @@ int main()
146
148
}
147
149
```
148
150
149
-
The preceding code declares a member function, `X`, in which the **this** pointer is treated as a **const** pointer to a **const** object. Combinations of *cv-mod-list* options can be used, but they always modify the object pointed to by **this**, not the **this**pointer itself. Therefore, the following declaration declares function `X`; the **this** pointer is a **const** pointer to a **const** object:
151
+
The preceding code declares a member function, `X`, in which the **this** pointer is treated as a **const** pointer to a **const** object. Combinations of *cv-mod-list* options can be used, but they always modify the object pointed to by the **this** pointer, not the pointer itself. The following declaration declares function `X`, where the **this** pointer is a **const** pointer to a **const** object:
150
152
151
153
```cpp
152
154
// type_of_this_pointer2.cpp
@@ -159,28 +161,30 @@ int main()
159
161
}
160
162
```
161
163
162
-
The type of **this** in a member function is described by the following syntax, where*cv-qualifier-list* is determined from the member functions declarator and can be **const** or **volatile** (or both), and*class-type* is the name of the class:
164
+
The type of **this** in a member function is described by the following syntax. The*cv-qualifier-list* is determined from the member function's declarator. It can be **const** or **volatile** (or both).*class-type* is the name of the class:
In other words, **this** is always a const pointer; it cannot be reassigned. The **const** or **volatile** qualifiers used in the member function declaration apply to the class instance pointed to by **this** in the scope of that function.
168
+
In other words, the **this**pointer is always a const pointer. It can't be reassigned. The **const** or **volatile** qualifiers used in the member function declaration apply to the class instance the **this** pointer points at, in the scope of that function.
167
169
168
170
The following table explains more about how these modifiers work.
169
171
170
-
### Semantics of this Modifiers
172
+
### Semantics of this modifiers
171
173
172
174
|Modifier|Meaning|
173
175
|--------------|-------------|
174
-
|**const**|Cannot change member data; cannot invoke member functions that are not**const**.|
175
-
|**volatile**|Member data is loaded from memory each time it is accessed; disables certain optimizations.|
176
+
|**const**|Can't change member data; can't invoke member functions that aren't**const**.|
177
+
|**volatile**|Member data is loaded from memory each time it's accessed; disables certain optimizations.|
176
178
177
-
It is an error to pass a **const** object to a member function that is not **const**. Similarly, it is an error to pass a **volatile** object to a member function that is not **volatile**.
179
+
It's an error to pass a **const** object to a member function that isn't **const**.
178
180
179
-
Member functions declared as **const** cannot change member data — in such functions, the **this** pointer is a pointer to a **const** object.
181
+
Similarly, it's also an error to pass a **volatile** object to a member function that isn't **volatile**.
182
+
183
+
Member functions declared as **const** can't change member data — in such functions, the **this** pointer is a pointer to a **const** object.
180
184
181
185
> [!NOTE]
182
-
> Constructors and destructors cannot be declared as **const** or **volatile**. They can, however, be invoked on **const** or **volatile** objects.
186
+
> Constructors and destructors can't be declared as **const** or **volatile**. They can, however, be invoked on **const** or **volatile** objects.
0 commit comments