在具有智能指针的类上正确实现Copy构造函数和Equals运算符 [英] Proper Implementation of Copy Constructor and Equals Operator on a class with smart pointers

查看:101
本文介绍了在具有智能指针的类上正确实现Copy构造函数和Equals运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我想实现一个可复制的类,那么我可以实现复制构造函数和赋值运算符.但是,唯一和共享指针变量的正确实现和处理方式是什么?请参阅以下人为设计的示例,其中包含两种类型的指针:

Suppose I want to implement a class which is copyable, so I can implement the copy constructor and assignment operator. However, what is the correct implementation and handling of unique and shared pointer variables? See this contrived example which has both types of pointers:

头文件

#include <memory>

using std::unique_ptr;
using std::shared_ptr;

class Copyable
{
private:
    unique_ptr<int> uniquePointer;
    shared_ptr<int> sharedPointer;

public:
    Copyable();
    Copyable(int value);
    Copyable(const Copyable& other);
    ~Copyable();

public:
    int GetUniqueValue() { return *uniquePointer; };
    int GetSharedValue() { return *sharedPointer; };
    Copyable& operator=(const Copyable& other);
};

CPP文件

#include "stdafx.h"
#include "Copyable.h"

using namespace std;

Copyable::Copyable() : 
    uniquePointer(make_unique<int>()), sharedPointer(make_shared<int>())
{
}

Copyable::Copyable(int value) : 
    uniquePointer(make_unique<int>(value)), 
    sharedPointer(make_shared<int>(value))
{
}

Copyable::Copyable(const Copyable& other) : 
    uniquePointer(make_unique<int>(*other.uniquePointer)), 
    sharedPointer(make_shared<int>(*other.sharedPointer))
    // OR
    sharedPointer(other.sharedPointer)
{
}

Copyable::~Copyable()
{
}

Copyable& Copyable::operator=(const Copyable& other)
{
    if (&other != this)
    {
        uniquePointer.reset();
        uniquePointer = make_unique<int>(*other.uniquePointer);

        sharedPointer = make_shared<int>(*other.sharedPointer);
        // OR
        sharedPointer = other.sharedPointer;
    }

    return *this;
}

使用允许复制

Copyable copyable1(5);
int uniqueValue1 = copyable1.GetUniqueValue();
int sharedValue1 = copyable1.GetSharedValue();
Copyable copyable2 = copyable1;
int uniqueValue2 = copyable2.GetSharedValue();
int sharedValue2 = copyable2.GetSharedValue();

只有一种方法可以使用make_unique函数复制唯一指针,但是共享指针呢?我应该分配它还是使用make_shared函数?

There is only one way to copy the unique pointer using the make_unique function but what about the shared pointer? Should I assign it or use the make_shared function?

更新-复制与移动

我想弄清楚何时使用什么更广泛的注释.如果我决定使用复制,为什么要使用unique_ptr?似乎shared_ptr是要走的路.同样,如果使用移动语义,unique_ptr似乎是可行的方法.一般而言.我也许应该将其分解为一个单独的问题.

One a wider note I'm trying to figure out when to use what. If I decide to use copying, why would I use unique_ptr? It seems shared_ptr is the way to go. Equally, if using move semantics, unique_ptr seems the way to go. Generally speaking only. I should perhaps split this into a separate question.

推荐答案

共享指针呢?我应该分配它还是使用make_shared函数?

What about the shared pointer? Should I assign it or use the make_shared function?

tl; dr最有可能是您要寻找的任务.

tl;dr The assignment is most likely what you are looking for.

这完全取决于所涉及类的语义;

This depends entirely on the semantics of the class involved;

  • 如果您希望对象共享shared_ptr的状态,则需要分配或副本
  • 如果您希望每个对象保持其自己的状态,请在其他"对象的基础上创建一个新的shared_ptr
  • If you want the objects to share the state of the shared_ptr then an assignment or a copy would be required
  • If you want each object to maintain it's own state, then create a new shared_ptr based on the "other" object

如果不需要共享状态,那么使用unique_ptr

If the shared state it not required, you really are just better off with a unique_ptr

作为一般的经验法则";

As a general "rule of thumb";

如果您的类型仅包含移动成员,则仅允许移动.如果您的类型具有可复制成员,则允许复制.在有意义的地方,遵循值"类型的语义.力争零规则"

If your type contains move only members, then only allow moving. If your type has copyable members, allow copying. Where it makes sense, follow the "value" type semantics. Strive for the "rule of zero"

这篇关于在具有智能指针的类上正确实现Copy构造函数和Equals运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆