Skip to content

Commit c009e1d

Browse files
Merge pull request #4855 from MicrosoftDocs/main638381754034811649sync_temp
For protected branch, push strategy should use PR and merge to target branch method to work around git push error
2 parents 0060999 + 6e475cc commit c009e1d

File tree

2 files changed

+20
-55
lines changed

2 files changed

+20
-55
lines changed

docs/cpp/primary-expressions.md

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
---
22
title: "Primary expressions"
33
description: "Primary expressions in the C++ programming language."
4-
ms.date: 10/02/2020
4+
ms.date: 12/11/2023
55
helpviewer_keywords: ["primary expressions", "expressions [C++], name", "expressions [C++], literal", "expressions [C++], primary", "expressions [C++], qualified names"]
6-
ms.assetid: 8ef9a814-6058-4b93-9b6e-e8eb8350b1ca
76
---
87
# Primary Expressions
98

@@ -15,27 +14,13 @@ Primary expressions are the building blocks of more complex expressions. They ma
1514
 *`name`*\
1615
 **`::`** *`name`* **`(`** *`expression`* **`)`**
1716

18-
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) .
1918

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.
2120

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).
2322

24-
```cpp
25-
// expre_Primary_Expressions.cpp
26-
// compile with: /LD
27-
class Example
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.
3924

4025
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.
4126

docs/cpp/this-pointer.md

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
---
22
title: "The this pointer"
33
description: "The this pointer is a compiler-generated pointer to the current object in nonstatic member functions."
4-
ms.date: "01/22/2020"
4+
ms.date: 12/11/2023
55
f1_keywords: ["this_cpp"]
66
helpviewer_keywords: ["nonstatic member functions [C++]", "pointers, to class instance", "this pointer"]
7-
ms.assetid: 92e3256a-4ad9-4d46-8be1-d77fad90791f
87
no-loc: [this, class, struct, union, sizeof, const, volatile]
98
---
109
# The `this` pointer
@@ -20,7 +19,7 @@ this->member-identifier
2019

2120
## Remarks
2221

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+
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:
2423

2524
```cpp
2625
myDate.setMonth( 3 );
@@ -59,7 +58,7 @@ if (&Object != this) {
5958
> [!NOTE]
6059
> Because the **`this`** pointer is nonmodifiable, assignments to the **`this`** pointer are not allowed. Earlier implementations of C++ allowed assignment to **`this`**.
6160
62-
Occasionally, the **`this`** pointer is used directlyfor example, to manipulate self-referential data structures, where the address of the current object is required.
61+
Occasionally, the **`this`** pointer is used directlyfor example, to manipulate self-referential data structures, where the address of the current object is required.
6362
6463
## Example
6564
@@ -133,41 +132,22 @@ your buffer
133132

134133
## Type of the `this` pointer
135134

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:
137136

138-
Consider an example:
139-
140-
```cpp
141-
// type_of_this_pointer1.cpp
142-
class Point
143-
{
144-
unsigned X() const;
145-
};
146-
int main()
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:
152-
153-
```cpp
154-
// type_of_this_pointer2.cpp
155-
class Point
156-
{
157-
unsigned X() const;
158-
};
159-
int main()
160-
{
161-
}
162-
```
137+
[*`cv-qualifier-list`*] *`class-type`* **`* const this`**
163138

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:
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.
165140

166-
[*`cv-qualifier-list`*] *`class-type`* **`* const this`**
141+
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:
167142

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` |
144+
|---|---|
145+
| `void Func()` | `myClass *` |
146+
| `void Func() const` | `const myClass *` |
147+
| `void Func() volatile` | `volatile myClass *` |
148+
| `void Func() const volatile` | `const volatile myClass *` |
169149

170-
The following table explains more about how these modifiers work.
150+
The following table explains more about `const` and `volatile``.
171151

172152
### Semantics of `this` modifiers
173153

@@ -180,7 +160,7 @@ It's an error to pass a **`const`** object to a member function that isn't **`co
180160

181161
Similarly, it's also an error to pass a **`volatile`** object to a member function that isn't **`volatile`**.
182162

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.
184164

185165
> [!NOTE]
186166
> Constructors and destructors can't be declared as **`const`** or **`volatile`**. They can, however, be invoked on **`const`** or **`volatile`** objects.

0 commit comments

Comments
 (0)