diff --git a/docs/cpp/primary-expressions.md b/docs/cpp/primary-expressions.md index 11f926e8a0f..8722190e3e4 100644 --- a/docs/cpp/primary-expressions.md +++ b/docs/cpp/primary-expressions.md @@ -1,9 +1,8 @@ --- title: "Primary expressions" description: "Primary expressions in the C++ programming language." -ms.date: 10/02/2020 +ms.date: 12/11/2023 helpviewer_keywords: ["primary expressions", "expressions [C++], name", "expressions [C++], literal", "expressions [C++], primary", "expressions [C++], qualified names"] -ms.assetid: 8ef9a814-6058-4b93-9b6e-e8eb8350b1ca --- # Primary Expressions @@ -15,27 +14,13 @@ Primary expressions are the building blocks of more complex expressions. They ma  *`name`*\  **`::`** *`name`* **`(`** *`expression`* **`)`** -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) . +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) . -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. +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. -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`**: +For more information about the type of the **`this`** pointer, see [`this` pointer](this-pointer.md). -```cpp -// expre_Primary_Expressions.cpp -// compile with: /LD -class Example -{ -public: - void Func(); // * const this - void Func() const; // const * const this - void Func() volatile; // volatile * const this -}; -``` - -For more information about modifying the type of the **`this`** pointer, see [`this` pointer](this-pointer.md). - -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. +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. 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. diff --git a/docs/cpp/this-pointer.md b/docs/cpp/this-pointer.md index ef9e1bb8595..1c1ea8223a0 100644 --- a/docs/cpp/this-pointer.md +++ b/docs/cpp/this-pointer.md @@ -1,10 +1,9 @@ --- title: "The this pointer" description: "The this pointer is a compiler-generated pointer to the current object in nonstatic member functions." -ms.date: "01/22/2020" +ms.date: 12/11/2023 f1_keywords: ["this_cpp"] helpviewer_keywords: ["nonstatic member functions [C++]", "pointers, to class instance", "this pointer"] -ms.assetid: 92e3256a-4ad9-4d46-8be1-d77fad90791f no-loc: [this, class, struct, union, sizeof, const, volatile] --- # The `this` pointer @@ -20,7 +19,7 @@ this->member-identifier ## Remarks -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: +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: ```cpp myDate.setMonth( 3 ); @@ -59,7 +58,7 @@ if (&Object != this) { > [!NOTE] > Because the **`this`** pointer is nonmodifiable, assignments to the **`this`** pointer are not allowed. Earlier implementations of C++ allowed assignment to **`this`**. -Occasionally, the **`this`** pointer is used directly — for example, to manipulate self-referential data structures, where the address of the current object is required. +Occasionally, the **`this`** pointer is used directly—for example, to manipulate self-referential data structures, where the address of the current object is required. ## Example @@ -133,41 +132,22 @@ your buffer ## Type of the `this` pointer -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. +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: -Consider an example: - -```cpp -// type_of_this_pointer1.cpp -class Point -{ - unsigned X() const; -}; -int main() -{ -} -``` - -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: - -```cpp -// type_of_this_pointer2.cpp -class Point -{ - unsigned X() const; -}; -int main() -{ -} -``` +[*`cv-qualifier-list`*] *`class-type`* **`* const this`** -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: +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. -[*`cv-qualifier-list`*] *`class-type`* **`* const this`** +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: -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. +| Member function declaration | type of `this` pointer for a class named `myClass` | +|---|---| +| `void Func()` | `myClass *` | +| `void Func() const` | `const myClass *` | +| `void Func() volatile` | `volatile myClass *` | +| `void Func() const volatile` | `const volatile myClass *` | -The following table explains more about how these modifiers work. +The following table explains more about `const` and `volatile``. ### Semantics of `this` modifiers @@ -180,7 +160,7 @@ It's an error to pass a **`const`** object to a member function that isn't **`co Similarly, it's also an error to pass a **`volatile`** object to a member function that isn't **`volatile`**. -Member functions declared as **`const`** can't change member data — in such functions, the **`this`** pointer is a pointer to a **`const`** object. +Member functions declared as **`const`** can't change member data. In `const` functions, the **`this`** pointer is a pointer to a **`const`** object. > [!NOTE] > Constructors and destructors can't be declared as **`const`** or **`volatile`**. They can, however, be invoked on **`const`** or **`volatile`** objects.