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
A *`literal`* is a constant primary expression. Its type depends on the form of its specification. For complete information about specifying literals, see [Literals](../cpp/numeric-boolean-and-pointer-literals-cpp.md) .
17
+
A *`literal`* is a constant primary expression. Its type depends on the form of its specification. For more information about specifying literals, see [Literals](../cpp/numeric-boolean-and-pointer-literals-cpp.md) .
19
18
20
-
The **`this`** keyword is a pointer to a class object. It's available within nonstatic member functions. It points to the instance of the class for which the function was invoked. The **`this`** keyword can't be used outside the body of a class-member function.
19
+
The **`this`** keyword is a pointer to an instance of the class. It's available within nonstatic member functions and points to the instance of the class the function was invoked from. The **`this`** keyword can't be used outside the body of a class-member function.
21
20
22
-
The type of the **`this`** pointer is `type * const` (where `type` is the class name) within functions that don't specifically modify the **`this`**pointer. The following example shows member function declarations and the types of **`this`**:
21
+
For more information about the type of the **`this`** pointer, see [`this` pointer](this-pointer.md).
23
22
24
-
```cpp
25
-
// expre_Primary_Expressions.cpp
26
-
// compile with: /LD
27
-
classExample
28
-
{
29
-
public:
30
-
void Func(); // * const this
31
-
void Func() const; // const * const this
32
-
void Func() volatile; // volatile * const this
33
-
};
34
-
```
35
-
36
-
For more information about modifying the type of the **`this`** pointer, see [`this` pointer](this-pointer.md).
37
-
38
-
The scope-resolution operator (**`::`**) followed by a name is a primary expression. Such names must be names at global scope, not member names. The type of the expression is determined by the declaration of the name. It's an l-value (that is, it can appear on the left-hand side of an assignment expression) if the declaring name is an l-value. The scope-resolution operator allows a global name to be referred to, even if that name is hidden in the current scope. See [Scope](../cpp/scope-visual-cpp.md) for an example of how to use the scope-resolution operator.
23
+
The scope-resolution operator (**`::`**) followed by a name is a primary expression. Such names must be names at global scope, not member names. The declaration of the name determines the type of the expression. It's an l-value (that is, it can appear on the left-hand side of an assignment expression) if the declaring name is an l-value. The scope-resolution operator allows a global name to be referred to, even if that name is hidden in the current scope. See [Scope](../cpp/scope-visual-cpp.md) for an example of how to use the scope-resolution operator.
39
24
40
25
An expression enclosed in parentheses is a primary expression. Its type and value are identical to the type and value of the unparenthesized expression. It's an l-value if the unparenthesized expression is an l-value.
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
+
An object's **`this`** pointer isn't part of the object itself. It's not part of 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:
24
23
25
24
```cpp
26
25
myDate.setMonth( 3 );
@@ -59,7 +58,7 @@ if (&Object != this) {
59
58
> [!NOTE]
60
59
> Because the **`this`** pointer is nonmodifiable, assignments to the **`this`** pointer are not allowed. Earlier implementations of C++ allowed assignment to **`this`**.
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
+
Occasionally, the **`this`** pointer is used directly—for example, to manipulate self-referential data structures, where the address of the current object is required.
63
62
64
63
## Example
65
64
@@ -133,41 +132,22 @@ your buffer
133
132
134
133
## Type of the `this` pointer
135
134
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
+
The **`this`** pointer's type changes depending on whether the function declaration includes the **`const`** and/or**`volatile`** keywords. The following syntax describes the type of **`this`** in a member function:
137
136
138
-
Consider an example:
139
-
140
-
```cpp
141
-
// type_of_this_pointer1.cpp
142
-
classPoint
143
-
{
144
-
unsigned X() const;
145
-
};
146
-
intmain()
147
-
{
148
-
}
149
-
```
150
-
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:
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:
139
+
The member function's declarator determines *`cv-qualifier-list`*. It can be **`const`** or **`volatile`** (or both). *`class-type`* is the name of the class.
The **`this`** pointer 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, as shown in the following table:
167
142
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.
143
+
| Member function declaration | type of `this` pointer for a class named `myClass`|
The following table explains more about how these modifiers work.
150
+
The following table explains more about `const` and `volatile``.
171
151
172
152
### Semantics of `this` modifiers
173
153
@@ -180,7 +160,7 @@ It's an error to pass a **`const`** object to a member function that isn't **`co
180
160
181
161
Similarly, it's also an error to pass a **`volatile`** object to a member function that isn't **`volatile`**.
182
162
183
-
Member functions declared as **`const`** can't change member data — in such functions, the **`this`** pointer is a pointer to a **`const`** object.
163
+
Member functions declared as **`const`** can't change member data. In `const` functions, the **`this`** pointer is a pointer to a **`const`** object.
184
164
185
165
> [!NOTE]
186
166
> Constructors and destructors can't be declared as **`const`** or **`volatile`**. They can, however, be invoked on **`const`** or **`volatile`** objects.
0 commit comments