带有智能指针的复制构造函数

Copy constructor with smart pointer

本文关键字:复制 构造函数 指针 智能      更新时间:2023-10-16

我有一个类,其中一个std::unique_ptr作为类成员。我想知道,如何正确定义复制构造函数,因为我得到以下编译器错误消息:error C2248: std::unique_ptr<_Ty>::unique_ptr : cannot access private member declared in class 'std::unique_ptr<_Ty>。我的类设计看起来像这样:

template <typename T>
class Foo{
    public:
        Foo(){};
        Foo( Bar<T> *, int );
        Foo( const Foo<T> & );
        ~Foo(){};
        void swap( Foo<T> & );
        Foo<T> operator = ( Foo<T> );
    private:
        std::unique_ptr<Bar> m_ptrBar;
        int m_Param1;
};
template < typename T >
Foo<T>::Foo( const Foo<T> & refFoo )
:m_ptrBar(refFoo.m_ptrBar), 
m_Param1(refFoo.m_Param1)
{
    // error here!
}
template < typename T >
void Foo<T>::swap( Foo<T> & refFoo ){
    using std::swap;
    swap(m_ptrBar, refFoo.m_ptrBar);
    swap(m_Param1, refFoo.m_Param1);
 }
 template < typename T >
 Foo<T> Foo<T>::operator = ( Foo<T> Elem ){
    Elem.swap(*this);
    return (*this);
 }

假设目标是复制唯一拥有的Bar,

template < typename T >
Foo<T>::Foo( const Foo<T> & refFoo )
: m_ptrBar(refFoo.m_ptrBar ? new Bar(*refFoo.m_ptrBar) : nullptr),
  m_Param1(refFoo.m_Param1)
{
}

Unique_ptr文档:

Stores a pointer to an owned object. The object is owned by no other unique_ptr. 
The object is destroyed when the unique_ptr is destroyed.

你不能复制它,因为两个对象不能拥有它。

尝试切换到std::shared_ptr.

EDIT我应该指出这将使两个对象具有指向同一对象的指针。如果你想复制唯一拥有的对象Cubbi的解决方案是正确的。

一种可能性是为此创建一个新的clone_ptr类型

下面是clone_ptr的基本示例,它调用派生对象的正确复制构造函数(和析构函数)。这是通过在创建clone_ptr时创建一个"类型擦除"帮助器来完成的。

可以在Internet上找到其他实现。

#include <memory>
namespace clone_ptr_detail
{
template <class T>
class clone_ptr_helper_base
{
public:
    virtual ~clone_ptr_helper_base() {}
    virtual T* clone(const T* source) const = 0;
    virtual void destroy(const T* p) const = 0;
};
template <class T, class U>
class clone_ptr_helper: public clone_ptr_helper_base<T>
{
public:
    virtual T* clone(const T* source) const
    {
        return new U(static_cast<const U&>(*source));
    }
    virtual void destroy(const T* p) const
    {
        delete static_cast<const U*>(p);
    }
};
}
template <class T>
class clone_ptr
{
    T* ptr;
    std::shared_ptr<clone_ptr_detail::clone_ptr_helper_base<T>> ptr_helper;
public:
    template <class U>
    explicit clone_ptr(U* p): ptr(p), ptr_helper(new clone_ptr_detail::clone_ptr_helper<T, U>()) {}
    clone_ptr(const clone_ptr& other): ptr(other.ptr_helper->clone(other.ptr)), ptr_helper(other.ptr_helper) {}
    clone_ptr& operator=(clone_ptr rhv)
    {
        swap(rhv);
        return *this;
    }
    ~clone_ptr()
    {
        ptr_helper->destroy(ptr);
    }
    T* get() const { /*error checking here*/ return ptr; }
    T& operator* () const { return *get(); }
    T* operator-> () const { return get(); }
    void swap(clone_ptr& other)
    {
        std::swap(ptr, other.ptr);
        ptr_helper.swap(other.ptr_helper);
    }
};

参见用法示例:http://ideone.com/LnWa3

(但也许你真的不需要复制你的对象,而可能更愿意探索移动语义的可能性。例如,你可以有一个vector<unique_ptr<T>>,只要你不使用复制内容的函数。