Skip to content

Commit 18496fd

Browse files
authored
Merge pull request #2264 from MicrosoftDocs/FromPublicMasterBranch
Confirm merge from FromPublicMasterBranch to master to sync with https://github.com/MicrosoftDocs/cpp-docs (branch master)
2 parents a06c730 + a534249 commit 18496fd

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

docs/build/how-to-use-build-events-in-msbuild-projects.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,18 @@ The following table lists each *use-in-build* element:
3232

3333
The following example can be added inside of the Project element of the myproject.vcxproj file created in [Walkthrough: Using MSBuild to Create a C++ Project](walkthrough-using-msbuild-to-create-a-visual-cpp-project.md). A *pre-build* event makes a copy of main.cpp; a *pre-link* event makes a copy of main.obj; and a *post-build* event makes a copy of myproject.exe. If the project is built using a release configuration, the build events are executed. If the project is built using a debug configuration, the build events are not executed.
3434

35-
```
35+
``` xml
3636
<ItemDefinitionGroup>
3737
<PreBuildEvent>
3838
<Command>copy $(ProjectDir)main.cpp $(ProjectDir)copyOfMain.cpp</Command>
3939
<Message>Making a copy of main.cpp </Message>
4040
</PreBuildEvent>
4141
<PreLinkEvent>
42-
<Command>copy $(ProjectDir)$(Configuration)\main.obj $(ProjectDir)$(Configuration)\copyOfMain.obj</Command>
42+
<Command>copy $(ProjectDir)$(Configuration)\main.obj $(ProjectDir)$(Configuration)\copyOfMain.obj</Command>
4343
<Message>Making a copy of main.obj</Message>
4444
</PreLinkEvent>
4545
<PostBuildEvent>
46-
<Command>copy $(ProjectDir)$(Configuration)\$(TargetFileName) $(ProjectDir)$(Configuration)\copyOfMyproject.exe</Command>
46+
<Command>copy $(ProjectDir)$(Configuration)\$(TargetFileName) $(ProjectDir)$(Configuration)\copyOfMyproject.exe</Command>
4747
<Message>Making a copy of myproject.exe</Message>
4848
</PostBuildEvent>
4949
</ItemDefinitionGroup>

docs/cpp/destructors-cpp.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: "Destructors (C++)"
3-
ms.date: "05/06/2019"
3+
ms.date: "07/20/2019"
44
helpviewer_keywords: ["objects [C++], destroying", "destructors, C++"]
55
ms.assetid: afa859b0-f3bc-4c4d-b250-c68b335b6004
66
---
@@ -215,4 +215,28 @@ s.~String(); // Virtual call
215215
ps->~String(); // Virtual call
216216
```
217217

218-
The notation for explicit calls to destructors, shown in the preceding, can be used regardless of whether the type defines a destructor. This allows you to make such explicit calls without knowing if a destructor is defined for the type. An explicit call to a destructor where none is defined has no effect.
218+
The notation for explicit calls to destructors, shown in the preceding, can be used regardless of whether the type defines a destructor. This allows you to make such explicit calls without knowing if a destructor is defined for the type. An explicit call to a destructor where none is defined has no effect.
219+
220+
## Robust programming
221+
222+
A class needs a destructor if it acquires a resource, and to manage the resource safely it probably has to implement a copy constructor and a copy assignment.
223+
224+
If these special functions are not defined by the user, they are implicitly defined by the compiler. The implicitly generated constructors and assignment operators perform shallow, memberwise copy, which is almost certainly wrong if an object is managing a resource.
225+
226+
In the next example, the implicitly generated copy constructor will make the pointers `str1.text` and `str2.text` refer to the same memory, and when we return from `copy_strings()`, that memory will be deleted twice, which is undefined behavior:
227+
228+
```cpp
229+
void copy_strings()
230+
{
231+
String str1("I have a sense of impending disaster...");
232+
String str2 = str1; // str1.text and str2.text now refer to the same object
233+
} // delete[] _text; deallocates the same memory twice
234+
// undefined behavior
235+
```
236+
237+
Explicitly defining a destructor, copy constructor, or copy assignment operator prevents implicit definition of the move constructor and the move assignment operator. In this case, failing to provide move operations is usually, if copying is expensive, a missed optimization opportunity.
238+
239+
## See also
240+
241+
[Copy Constructors and Copy Assignment Operators](../cpp/copy-constructors-and-copy-assignment-operators-cpp.md)</br>
242+
[Move Constructors and Move Assignment Operators](../cpp/move-constructors-and-move-assignment-operators-cpp.md)

0 commit comments

Comments
 (0)