Rule of three

The “Rule of Three” is a guideline in C++ that suggests implementing three special member functions when you have a user-defined class that manages dynamically allocated resources. These special member functions are the destructor, copy constructor, and copy assignment operator. Let’s explore each of them and provide an example along with a diagram to illustrate their usage.

  1. Destructor: The destructor is responsible for releasing any resources (like dynamically allocated memory) held by an object when it goes out of scope or is explicitly destroyed. It is declared with a tilde (~) followed by the class name. Here’s an example:
class MyClass {
private:
    int* data;

public:
    MyClass() {
        data = new int[10];
        // Initialize data
    }

    ~MyClass() {
        delete[] data;
    }
};

Diagram:

        +----------------+
        |     MyClass    |
        +----------------+
        |     - data     |
        +----------------+
               |  |
               |  |
               v  v
        +----------------+
        |    ~MyClass    |
        +----------------+
  1. Copy Constructor: The copy constructor is used to create a new object as a copy of an existing object of the same class. It is invoked when an object is initialized using another object of the same class or passed by value. Here’s an example:
class MyClass {
private:
    int* data;
    int size;

public:
    MyClass(const MyClass& other) {
        size = other.size;
        data = new int[size];
        // Copy data from 'other' to 'data'
    }
};

Diagram:

         +----------------+
         |    MyClass     |
         +----------------+
         |    - data      |
         |    - size      |
         +----------------+
               |  |
               |  |
               v  v
         +----------------+
         | MyClass(const MyClass& other) |
         +----------------+
  1. Copy Assignment Operator: The copy assignment operator is used to assign the values of one object to another object of the same class. It is invoked when an assignment operation is performed between two objects of the class. Here’s an example:
class MyClass {
private:
    int* data;
    int size;

public:
    MyClass& operator=(const MyClass& other) {
        if (this != &other) {
            delete[] data;
            size = other.size;
            data = new int[size];
            // Copy data from 'other' to 'data'
        }
        return *this;
    }
};

Diagram:

        +----------------+
        |    MyClass     |
        +----------------+
        |    - data      |
        |    - size      |
        +----------------+
               |  |
               |  |
               v  v
         +----------------+
         |operator=(const MyClass& other)|
         +----------------+

By implementing these three special member functions, you ensure that your class properly manages its resources when it comes to copying and destruction. This follows the Rule of Three, which states that if a class requires one of these functions, it usually requires all three.

Please note that with the introduction of C++11 and later versions, the Rule of Three is extended to the Rule of Five to accommodate move semantics, involving the move constructor and move assignment operator.

Leave a Reply

Your email address will not be published. Required fields are marked *