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
### 2.8 Rules for Object Management <a name="2.8"></a>
696
+
697
+
[go to top](#top)
698
+
699
+
#### **Introduction**
700
+
701
+
C++ provides 6 special functions that are called default operations that deal with managing an object's lifecycle.
702
+
703
+
| Operation | Signature |
704
+
| :-----------------: | :-------------------: |
705
+
| Default constructor | `X()` |
706
+
| Copy constructor | `X(const X&)` |
707
+
| Copy assignment | `operator=(const X&)` |
708
+
| Move constructor | `X(X&&)` |
709
+
| Move assignment | `operator=(X&&)` |
710
+
| Destructor | `~X()` |
711
+
712
+
You can explicitly define them, delete them, or lean on the compiler's default.
713
+
714
+
```c++
715
+
X() {}; // Default constructor, explicitly defined
716
+
X() = default; // Default constructor, defaulting to compiler implementation
717
+
X() = delete; // Explicitly deleted default constructor. There will be no implementation
718
+
```
719
+
720
+
The rules for object management help to keep your programs running as expected. You can treat the rules like boilerplate, but just keep them in mind.
721
+
722
+
723
+
724
+
#### **Rule of Zero**
725
+
726
+
This one is fairly simple. Since it keeps things simple.
727
+
728
+
> **If you can avoid redefining any default operations, do.**
729
+
730
+
731
+
732
+
#### **Rule of Three**
733
+
734
+
> **If you redefine at least one of the following operations: Destructor, Copy Constructor, Copy Assignment, explicitly redefine all three.**
735
+
736
+
Example:
737
+
738
+
- Suppose we have a classwith a redefined destructor `~X()`
739
+
- When the default copy assignment `operator=(const X&)` or copy constructor `X(const X&)` is used, it copies all class members, including the redefined destructor, and **any declared local pointers without reallocating extra memory for those copied local pointers**.
740
+
- In other words, using the default copy assignment and copy constructor operations, all pointers local to each class instance point to the same piece of memory!
741
+
- So when the program goes out of scope, for the same destructor is called for all copies of the object, the same address pointed to by all pointers in instances of the class are deleted multiple times, crashing the program.
742
+
743
+
Similar logic can be used for the other two operations. If you only define one copy operation, the other gets used as the default and will really mess up your memory management.
744
+
745
+
746
+
747
+
#### **Rule of Five**
748
+
749
+
Actually all six operations are intimately linked. So the rule of five (it's the rule of six if you count the default constructor) exists to remind you that...
750
+
751
+
> **If you redefine or =delete any default operation, define or =delete them all.**
752
+
753
+
If you don't follow the rule, your objects might behave weirdly.
754
+
755
+
Furthermore, when you actually define them, make sure you use **consistent semantics**. (Example: If you use shallow copies (copying of pointers) in a single copy operation, do not use deep copies (copying pointers and pointed data) for the other copy operator))
756
+
757
+
(You'll generally want to use deep copies usually anyway...)
0 commit comments