Managing Memory Manually

Rule of Three and std::unique_ptr

How does the Rule of Three apply when using std::unique_ptr for resource management?

Abstract art representing computer programming

The Rule of Three in C++ is a guideline that states if a class needs to define one of the following special member functions, it likely needs to define all three:

  1. Destructor
  2. Copy Constructor
  3. Copy Assignment Operator

How the Rule of Three Applies

The Rule of Three is traditionally concerned with managing resource ownership and cleanup. However, when using std::unique_ptr, the Rule of Three changes slightly:

Destructor

With std::unique_ptr, you don’t need to define a custom destructor unless you need to perform additional cleanup. std::unique_ptr automatically handles the deletion of the managed resource when the object is destroyed.

class MyClass {
 public:
  std::unique_ptr<int> ptr;
  MyClass() : ptr(std::make_unique<int>(42)) {}
  // Destructor is automatically generated
  // and manages resource cleanup
};

Copy Constructor

std::unique_ptr is not copyable. It enforces unique ownership, so copying is not allowed. If you attempt to copy a std::unique_ptr, it will result in a compile-time error.

MyClass obj1;

// Error: std::unique_ptr cannot be copied
MyClass obj2 = obj1;

Copy Assignment Operator

Similar to the copy constructor, the copy assignment operator is deleted for std::unique_ptr. You can only move std::unique_ptr, which transfers ownership.

MyClass obj1;
MyClass obj2;

// Correct: moves ownership from obj1 to obj2
obj2 = std::move(obj1);

Move Semantics

Since std::unique_ptr does not allow copying, it relies on move semantics:

Move Constructor: Transfers ownership from one std::unique_ptr to another.

std::unique_ptr<int> ptr1{
  std::make_unique<int>(42)};
  
// Ownership transferred to ptr2
std::unique_ptr<int> ptr2{
  std::move(ptr1)};

Move Assignment Operator: Assigns ownership from one std::unique_ptr to another.

std::unique_ptr<int> ptr1{
  std::make_unique<int>(42)};
std::unique_ptr<int> ptr2;

// Ownership transferred to ptr2
ptr2 = std::move(ptr1);

Conclusion

When using std::unique_ptr, the Rule of Three does not apply in the traditional sense since std::unique_ptr is non-copyable and manages its own resource cleanup.

Instead, focus on understanding and using move semantics with std::unique_ptr for efficient resource management.

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

3D art showing a progammer setting up a development environment
Part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

Free, unlimited access

This course includes:

  • 59 Lessons
  • Over 200 Quiz Questions
  • 95% 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