繁体   English   中英

智能指针如何影响 5 规则?

[英]How do smart pointers affect the rule of 5?

我了解到,当您在 class 中使用指针时,您应该执行 5 规则。如果您不使用指针,那么您可以,实际上更可取的是使用默认值。 但是,这如何与智能指针一起使用? 例如,包含int*的 class 可能如下所示:

class A {
private:
    int *num_;
public:

    explicit A(int* num) : num_(num) {}

    ~A() {
        delete num_;
    }

    A(const A &other) {
        if (this != &other) {
            num_ = other.num_;
        }
    }

    A(A &&other) noexcept {
        if (this != &other) {
            num_ = other.num_;
        }
    }

    A &operator=(A &other) {
        if (this == &other) {
            this->num_ = other.num_;
        }
        return *this;
    }

    A &operator=(A &&other) noexcept {
        if (this == &other) {
            this->num_ = other.num_;
        }
        return *this;
    };


};

但是如果我们使用智能指针,这样做就足够了吗?

class B {
private:
    std::unique_ptr<int> num_;

public:

    explicit B(int num) : num_(std::make_unique<int>(num)) {};

};

是的,这就足够了。 唯一指针确实管理 memory。 但是,您的两个类的行为会有所不同,因为std::unique_ptr无法复制,因此不会有编译器生成的复制构造函数,也不会为B赋值。

另请注意,您实现了规则 5 的所有方法,但不正确。 如评论中所述,复制A将导致两个实例具有相同的指针并在销毁时将其删除。 实际上,做到这一点是关于 3/5 规则的全部意义,以及为什么你应该更喜欢 0 规则。

如果您使用智能指针(或任何 std:: 容器),class 默认析构函数将调用智能指针(和容器)的析构函数。 有关此主题的更多信息: 为什么 C++ 默认析构函数不破坏我的对象?

那些有不同的行为。 A可以复制, B只能移动。

注意您的A实现是不安全的,它可能导致泄漏和未定义的行为。

同类比较会delete A的副本

class A {
private:
    int *num_;
public:

    explicit A(int num) : num_(new int(num)) {}

    ~A() {
        delete num_;
    }

    A(const A &other) = delete;

    A(A &&other) noexcept 
     : num_(std::exchange(other.num, nullptr)) {}

    A &operator=(const A &other) =delete;

    A &operator=(A &&other) noexcept {
        swap(num_, other.num_);
        return *this;
    };
};

class B {
private:
    std::unique_ptr<int> num_;

public:

    explicit B(int num) : num_(std::make_unique<int>(num)) {};

};

或者定义B的副本

class A {
private:
    int *num_;
public:

    explicit A(int num) : num_(new int(num)) {}

    ~A() {
        delete num_;
    }

    A(const A &other) 
     : A(other.num) {}

    A(A &&other) noexcept 
     : num_(std::exchange(other.num, nullptr)) {}

    A &operator=(const A &other) {
        *num_ = *other.num;
        return *this;
    }

    A &operator=(A &&other) noexcept {
        swap(num_, other.num_);
        return *this;
    };
};

class B {
private:
    std::unique_ptr<int> num_;

public:

    explicit B(int num) : num_(std::make_unique<int>(num)) {};
    ~B() = default;
    B(const B & other) : B(*other.num_) {}
    B(B && other) = default;
    B& operator=(const B & other) { *num_ = *other.num_ }
    B& operator=(B && other) = default;

};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM