Move Semantics

The Rule of Five

What is the Rule of Five in C++, and how does it relate to move semantics?

Illustration representing computer hardware

The Rule of Five is a guideline in C++ that suggests that if a class defines any of the following special member functions, it should probably define all five:

  1. Destructor
  2. Copy constructor
  3. Copy assignment operator
  4. Move constructor
  5. Move assignment operator

The reasoning behind this rule is that these functions are usually interconnected. If a class needs to define one of them for correct operation, it likely needs to define the others as well.

For example, if a class manages a resource (like dynamically allocated memory) and defines a custom destructor to release that resource, it should also define a copy constructor and copy assignment operator to properly copy that resource. Otherwise, copying the object could lead to issues like double-deletion of the resource.

When C++11 introduced move semantics, the Rule of Five was born as an extension of the Rule of Three (which covered only the destructor, copy constructor, and copy assignment operator). If a class defines a move constructor or move assignment operator, it's probably because it manages a resource that can be efficiently moved. In that case, it should also define the destructor and copy operations for completeness.

Here's an example of a class that follows the Rule of Five:

class Resource {
 public:
  // Default constructor
  Resource();

  // Destructor
  ~Resource();

  // Copy constructor
  Resource(const Resource& other);

  // Copy assignment
  Resource& operator=(const Resource& other);

  // Move constructor
  Resource(Resource&& other);

  // Move assignment
  Resource& operator=(Resource&& other);

 private:
  // ...
};

By defining all five special member functions, Resource ensures that it can be constructed, destroyed, copied, and moved correctly, providing a complete and consistent interface.

This Question is from the Lesson:

Move Semantics

Learn how we can improve the performance of our types using move constructors, move assignment operators and std::move()

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

Move Semantics

Learn how we can improve the performance of our types using move constructors, move assignment operators and std::move()

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 124 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved