编程参考 - Rule of Three and the Rule of Five in C++

C++ 中的 "三规则 "和 "五规则 "是管理类中资源管理函数(特殊成员函数)的准则。这些规则有助于确保类正确一致地管理动态内存、文件句柄或网络连接等资源。

The Rule of Three and the Rule of Five in C++ are guidelines for managing the resource management functions (special member functions) in a class. These rules help ensure that classes manage resources like dynamic memory, file handles, or network connections correctly and consistently.

Rule of Three

"三原则"指出,如果一个类定义了以下内容之一,那么它可能应该明确定义所有三项内容:

The Rule of Three states that if a class defines one of the following, it should probably explicitly define all three:

1, Destructor

2, Copy Constructor

3, Copy Assignment Operator

之所以会出现这条规则,是因为如果一个类管理一个资源(如动态内存),编译器提供的这些函数的默认实现可能无法正确处理该资源,从而导致重复删除或资源泄漏等问题。

This rule arises because if a class manages a resource (like dynamic memory), the default implementations of these functions provided by the compiler might not handle the resource correctly, leading to issues like double deletion or resource leaks.

Example:

#include <iostream>

class RuleOfThree {

private:

    int* data;

public:

    RuleOfThree(int value) : data(new int(value)) {}

    ~RuleOfThree() {

        delete data;

    }

    // Copy constructor

    RuleOfThree(const RuleOfThree& other) : data(new int(*other.data)) {}

    // Copy assignment operator

    RuleOfThree& operator=(const RuleOfThree& other) {

        if (this == &other) {

            return *this;

        }

        delete data;

        data = new int(*other.data);

        return *this;

    }

};

Rule of Five

随着 C++11 的推出,移动语义被添加到语言中,并由此产生了 "五法则"。五规则扩展了三规则,将移动操作也包括在内:

With the introduction of C++11, move semantics were added to the language, which led to the Rule of Five. The Rule of Five extends the Rule of Three to include move operations:

1, Destructor

2, Copy Constructor

3, Copy Assignment Operator

4, Move Constructor

5, Move Assignment Operator

对于管理资源的类来说,移动操作是必要的,因为它可以从临时对象中有效地转移资源,避免不必要的深度拷贝。

Move operations are necessary for classes that manage resources because they allow efficient transfer of resources from temporary objects, avoiding unnecessary deep copies.

Example:

#include <iostream>

class RuleOfFive {

private:

    int* data;

public:

    RuleOfFive(int value) : data(new int(value)) {}

    ~RuleOfFive() {

        delete data;

    }

    // Copy constructor

    RuleOfFive(const RuleOfFive& other) : data(new int(*other.data)) {}

    // Copy assignment operator

    RuleOfFive& operator=(const RuleOfFive& other) {

        if (this == &other) {

            return *this;

        }

        delete data;

        data = new int(*other.data);

        return *this;

    }

    // Move constructor

    RuleOfFive(RuleOfFive&& other) noexcept : data(other.data) {

        other.data = nullptr;

    }

    // Move assignment operator

    RuleOfFive& operator=(RuleOfFive&& other) noexcept {

        if (this == &other) {

            return *this;

        }

        delete data;

        data = other.data;

        other.data = nullptr;

        return *this;

    }

};

Summary

  • 三原则: 如果你定义了析构函数、复制构造函数或复制赋值操作符中的任何一种,你可能应该定义所有三种。

  • 五条规则: 在 C++11 中增加了移动语义后,如果您定义了析构函数、复制构造函数、复制赋值操作符、移动构造函数或移动赋值操作符中的任何一种,则应定义所有五种。

这些规则有助于确保您的类正确管理其资源,避免资源泄露、重复删除和低效深度拷贝等问题。

  • Rule of Three: If you define any of the destructor, copy constructor, or copy assignment operator, you should probably define all three.

  • Rule of Five: With the addition of move semantics in C++11, if you define any of the destructor, copy constructor, copy assignment operator, move constructor, or move assignment operator, you should define all five.

These rules help ensure that your class correctly manages its resources, avoiding issues like resource leaks, double deletions, and inefficient deep copies.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜流冰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值