Resource Acquisition and the Rule of Five
Learn about the concept of the rule of five with implementing functions.
We'll cover the following
Securing resources through the rule of five
To fully understand move semantics, we need to go back to the basics of classes and resource acquisition in C++. One of the basic concepts in C++ is that a class should completely handle its resources. This means that when a class is copied, moved, copy-assigned, move-assigned, or destructed, the class should make sure its resources are handled accordingly. The necessity of implementing these five functions is commonly referred to as the rule of five.
Let's have a look at how the rule of five can be implemented in a class handling an allocated resource. In the Buffer
class defined in the following code playground, the allocated resource is an array of floats pointed at by the raw pointer ptr_
:
Get hands-on with 1400+ tech skills courses.