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 default implementation of `OnFinalMessage` does nothing, but you can override this function to handle cleanup before destroying a window. If you want to automatically delete your object upon the window destruction, you can call **delete this;** in this function.
279
+
The default implementation of `OnFinalMessage` does nothing, but you can override this function to handle cleanup before destroying a window. If you want to automatically delete your object upon the window destruction, you can call `delete this;` in this function.
Copy file name to clipboardExpand all lines: docs/cpp/function-overloading.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -368,7 +368,7 @@ Even though the second one requires both a standard conversion and the user-defi
368
368
> [!NOTE]
369
369
> User-defined conversions are considered conversion by construction or conversion by initialization (conversion function). Both methods are considered equal when considering the best match.
370
370
371
-
## Argument matching and the this pointer
371
+
## Argument matching and the `this` pointer
372
372
373
373
Class member functions are treated differently, depending on whether they are declared as **`static`**. Because nonstatic functions have an implicit argument that supplies the **`this`** pointer, nonstatic functions are considered to have one more argument than static functions; otherwise, they are declared identically.
Copy file name to clipboardExpand all lines: docs/cpp/object-lifetime-and-resource-management-modern-cpp.md
+9-10Lines changed: 9 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,13 +1,13 @@
1
1
---
2
2
title: "Object lifetime and resource management (RAII)"
3
3
description: "Follow the principle of RAII in modern C++ to avoid resource leaks."
4
-
ms.date: "11/19/2019"
4
+
ms.date: 06/02/2022
5
5
ms.topic: "conceptual"
6
6
ms.assetid: 8aa0e1a1-e04d-46b1-acca-1d548490700f
7
7
---
8
8
# Object lifetime and resource management (RAII)
9
9
10
-
Unlike managed languages, C++ doesn't have automatic *garbage collection*. That's an internal process that releases heap memory and other resources as a program runs. A C++ program is responsible for returning all acquired resources to the operating system. Failure to release an unused resource is called a *leak*. Leaked resources are unavailable to other programs until the process exits. Memory leaks in particular are a common cause of bugs in C-style programming.
10
+
Unlike managed languages, C++ doesn't have automatic *garbage collection*, an internal process that releases heap memory and other resources as a program runs. A C++ program is responsible for returning all acquired resources to the operating system. Failure to release an unused resource is called a *leak*. Leaked resources are unavailable to other programs until the process exits. Memory leaks in particular are a common cause of bugs in C-style programming.
11
11
12
12
Modern C++ avoids using heap memory as much as possible by declaring objects on the stack. When a resource is too large for the stack, then it should be *owned* by an object. As the object gets initialized, it acquires the resource it owns. The object is then responsible for releasing the resource in its destructor. The owning object itself is declared on the stack. The principle that *objects own resources* is also known as "resource acquisition is initialization," or RAII.
13
13
@@ -48,7 +48,7 @@ public:
48
48
};
49
49
50
50
void functionUsingWidget() {
51
-
widget w(1000000); // lifetime automatically tied to enclosing scope
51
+
widget w(1000000); // lifetime automatically tied to enclosing scope
52
52
// constructs w, including the w.data member
53
53
w.do_something();
54
54
@@ -63,20 +63,19 @@ Since C++11, there's a better way to write the previous example: by using a smar
63
63
classwidget
64
64
{
65
65
private:
66
-
std::unique_ptr<int> data;
66
+
std::unique_ptr<int[]> data;
67
67
public:
68
-
widget(const int size) { data = std::make_unique<int>(size); }
68
+
widget(const int size) { data = std::make_unique<int[]>(size); }
69
69
void do_something() {}
70
70
};
71
71
72
72
voidfunctionUsingWidget() {
73
-
widget w(1000000); // lifetime automatically tied to enclosing scope
74
-
// constructs w, including the w.data gadget member
73
+
widget w(1000000); // lifetime automatically tied to enclosing scope
74
+
// constructs w, including the w.data gadget member
75
75
// ...
76
76
w.do_something();
77
77
// ...
78
78
} // automatic destruction and deallocation for w and w.data
79
-
80
79
```
81
80
82
81
By using smart pointers for memory allocation, you may eliminate the potential for memory leaks. This model works for other resources, such as file handles or sockets. You can manage your own resources in a similar way in your classes. For more information, see [Smart pointers](smart-pointers-modern-cpp.md).
@@ -85,6 +84,6 @@ The design of C++ ensures objects are destroyed when they go out of scope. That
85
84
86
85
## See also
87
86
88
-
[Welcome back to C++](../cpp/welcome-back-to-cpp-modern-cpp.md)<br/>
89
-
[C++ Language Reference](../cpp/cpp-language-reference.md)<br/>
87
+
[Welcome back to C++](../cpp/welcome-back-to-cpp-modern-cpp.md)\
88
+
[C++ language reference](../cpp/cpp-language-reference.md)\
90
89
[C++ Standard Library](../standard-library/cpp-standard-library-reference.md)
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.
13
13
@@ -131,7 +131,7 @@ my buffer
131
131
your buffer
132
132
```
133
133
134
-
## Type of the this pointer
134
+
## Type of the `this` pointer
135
135
136
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.
137
137
@@ -161,15 +161,15 @@ int main()
161
161
}
162
162
```
163
163
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:
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, 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.
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.
169
169
170
170
The following table explains more about how these modifiers work.
Copy file name to clipboardExpand all lines: docs/error-messages/compiler-errors-1/compiler-error-c2107.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,7 +14,7 @@ A subscript is applied to an expression that does not evaluate to a pointer.
14
14
15
15
## Example
16
16
17
-
C2107 can occur if you incorrectly use the **`this`** pointer of a value type to access the type's default indexer. For more information, see [Semantics of the this pointer](../../dotnet/how-to-define-and-consume-classes-and-structs-cpp-cli.md#BKMK_Semantics_of_the_this_pointer).
17
+
C2107 can occur if you incorrectly use the **`this`** pointer of a value type to access the type's default indexer. For more information, see [Semantics of the `this` pointer](../../dotnet/how-to-define-and-consume-classes-and-structs-cpp-cli.md#BKMK_Semantics_of_the_this_pointer).
description: "Learn more about: Compiler Error C2834"
3
3
title: "Compiler Error C2834"
4
-
ms.date: "11/04/2016"
4
+
ms.date: 06/01/2022
5
5
f1_keywords: ["C2834"]
6
6
helpviewer_keywords: ["C2834"]
7
7
ms.assetid: 28f9f6eb-ab2a-4e64-aaaa-9d14f955de41
8
8
---
9
9
# Compiler Error C2834
10
10
11
-
'operator operator' must be globally qualified
11
+
> 'operator *operator-name*' must be globally qualified
12
12
13
13
The `new` and `delete` operators are tied to the class where they reside. Scope resolution cannot be used to select a version of `new` or `delete` from a different class. To implement multiple forms of the `new` or `delete` operator, create a version of the operator with extra formal parameters.
14
+
15
+
This error is obsolete in Visual Studio 2022 and later versions.
description: "Learn more about: Compiler Error C2858"
3
3
title: "Compiler Error C2858"
4
-
ms.date: "11/04/2016"
4
+
ms.date: 06/01/2022
5
5
f1_keywords: ["C2858"]
6
6
helpviewer_keywords: ["C2858"]
7
7
ms.assetid: 1fb1d770-307e-476e-9984-a1d8f8ce2820
8
8
---
9
9
# Compiler Error C2858
10
10
11
-
command-line option '/Yc (/Fdfilename)' inconsistent with precompiled header, which used '/Fdfilename'
11
+
> command-line option '/Yc (/Fd*filename1*)' inconsistent with precompiled header, which used '/Fd*filename2*'
12
12
13
-
The program database specified by the Use Precompiled Header ([/Yu](../../build/reference/yu-use-precompiled-header-file.md)) option is not the one specified by the previous Create Precompiled Header ([/Yc](../../build/reference/yc-create-precompiled-header-file.md)) option.
13
+
The program database specified by the Use Precompiled Header ([`/Yu`](../../build/reference/yu-use-precompiled-header-file.md)) option is not the one specified by the previous Create Precompiled Header ([`/Yc`](../../build/reference/yc-create-precompiled-header-file.md)) option.
14
+
15
+
This error is obsolete in Visual Studio 2022 and later versions.
0 commit comments