Skip to content

Commit db1c552

Browse files
authored
Update destructors-cpp.md
A little edit pass
1 parent b988817 commit db1c552

File tree

1 file changed

+5
-26
lines changed

1 file changed

+5
-26
lines changed

docs/cpp/destructors-cpp.md

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
---
22
description: "Learn more about: Destructors (C++)"
33
title: "Destructors (C++)"
4-
ms.date: "07/20/2019"
4+
ms.date: "11/29/2023"
55
helpviewer_keywords: ["objects [C++], destroying", "destructors, C++"]
66
---
77
# Destructors (C++)
88

9-
A destructor is a member function that is invoked automatically when the object goes out of scope or is explicitly destroyed by a call to **`delete`** or **`delete[]`**. A destructor has the same name as the class, preceded by a tilde (`~`). For example, the destructor for class `String` is declared: `~String()`.
9+
A destructor is a member function that is invoked automatically when the object goes out of scope or is explicitly destroyed by a call to **`delete`** or **`delete[]`**. A destructor has the same name as the class and is preceded by a tilde (`~`). For example, the destructor for class `String` is declared: `~String()`.
1010

11-
If you don't define a destructor, the compiler provides a default one; for many classes this is sufficient. You only need to define a custom destructor when the class stores handles to system resources that need to be released, or pointers that own the memory they point to.
11+
If you don't define a destructor, the compiler provides a default one, and for some classes this is sufficient. You need to define a custom destructor when the class maintains resources that must be explicitly released, such as handles to system resources or pointers to memory that should be released when an instance of the class is destroyed.
1212

1313
Consider the following declaration of a `String` class:
1414

@@ -58,27 +58,19 @@ Destructors are functions with the same name as the class but preceded by a tild
5858
Several rules govern the declaration of destructors. Destructors:
5959
6060
- Don't accept arguments.
61-
6261
- Don't return a value (or **`void`**).
63-
6462
- Can't be declared as **`const`**, **`volatile`**, or **`static`**. However, they can be invoked for the destruction of objects declared as **`const`**, **`volatile`**, or **`static`**.
65-
6663
- Can be declared as **`virtual`**. Using virtual destructors, you can destroy objects without knowing their type—the correct destructor for the object is invoked using the virtual function mechanism. Destructors can also be declared as pure virtual functions for abstract classes.
6764
6865
## Using destructors
6966
7067
Destructors are called when one of the following events occurs:
7168
7269
- A local (automatic) object with block scope goes out of scope.
73-
74-
- An object allocated using the **`new`** operator is explicitly deallocated using **`delete`**. If **`delete[]`** is used instead, it would result in undefined behaviour.
75-
76-
- Objects allocated using the **`new[]`** operator is explicitly deallocated using **`delete[]`**. If **`delete`** is used instead, it would result in undefined behaviour.
77-
70+
- Use **`delete`** to deallocate an object allocated using **`new`**. Using **`delete[]`** results in undefined behaviour.
71+
- Use **`delete[]`** to deallocate an object allocated using **`new[]`**. Using **`delete`** results in undefined behaviour.
7872
- The lifetime of a temporary object ends.
79-
8073
- A program ends and global or static objects exist.
81-
8274
- The destructor is explicitly called using the destructor function's fully qualified name.
8375
8476
Destructors can freely call class member functions and access class member data.
@@ -160,35 +152,22 @@ class E : public C, public D, virtual public B {};
160152
To determine the order of destruction of the virtual base classes of an object of type `E`, the compiler builds a list by applying the following algorithm:
161153
162154
1. Traverse the graph left, starting at the deepest point in the graph (in this case, `E`).
163-
164155
1. Perform leftward traversals until all nodes have been visited. Note the name of the current node.
165-
166156
1. Revisit the previous node (down and to the right) to find out whether the node being remembered is a virtual base class.
167-
168157
1. If the remembered node is a virtual base class, scan the list to see whether it has already been entered. If it isn't a virtual base class, ignore it.
169-
170158
1. If the remembered node isn't yet in the list, add it to the bottom of the list.
171-
172159
1. Traverse the graph up and along the next path to the right.
173-
174160
1. Go to step 2.
175-
176161
1. When the last upward path is exhausted, note the name of the current node.
177-
178162
1. Go to step 3.
179-
180163
1. Continue this process until the bottom node is again the current node.
181164
182165
Therefore, for class `E`, the order of destruction is:
183166
184167
1. The non-virtual base class `E`.
185-
186168
1. The non-virtual base class `D`.
187-
188169
1. The non-virtual base class `C`.
189-
190170
1. The virtual base class `B`.
191-
192171
1. The virtual base class `A`.
193172
194173
This process produces an ordered list of unique entries. No class name appears twice. Once the list is constructed, it's walked in reverse order, and the destructor for each of the classes in the list from the last to the first is called.

0 commit comments

Comments
 (0)