簡體   English   中英

使用智能指針復制構造函數

[英]Copy constructor with smart pointer

我有一個類,其中一個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。

編輯我應該指出,這將使兩個對象都有一個指向同一個對象的指針。 如果你想復制唯一擁有的對象,Cubbi的解決方案是正確的。

可能是為此創建一個新的clone_ptr類型。

下面是clone_ptr一個基本示例,它調用派生對象的正確復制構造函數(和析構函數)。 這是通過在創建clone_ptr時創建“類型擦除”幫助clone_ptr來完成的。

可以在因特網上找到其他實現。

#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>> ,只要你不使用復制的函數內容。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM