[英]Copy constructor with smart pointer
I have a class with one std::unique_ptr
as class member. 我有一个类,其中一个
std::unique_ptr
作为类成员。 I was wondering, how to correctly define the copy constructor, since I'm getting the following compiler error message: error C2248: std::unique_ptr<_Ty>::unique_ptr : cannot access private member declared in class 'std::unique_ptr<_Ty>
. 我想知道,如何正确定义复制构造函数,因为我收到以下编译器错误消息:
error C2248: std::unique_ptr<_Ty>::unique_ptr : cannot access private member declared in class 'std::unique_ptr<_Ty>
。 My class design looks something like: 我的班级设计看起来像:
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);
}
Assuming the goal is to copy-construct the uniquely-owned Bar, 假设目标是复制 - 构建独特拥有的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 documentation: 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.
You cant copy it because two objects can't own it. 你无法复制它,因为两个对象不能拥有它。
Try switching to a std::shared_ptr. 尝试切换到std :: shared_ptr。
EDIT I should point out that this would make both objects have a pointer to that same object. 编辑我应该指出,这将使两个对象都有一个指向同一个对象的指针。 If you want to copy the uniquely owned object Cubbi's solution is the correct one.
如果你想复制唯一拥有的对象,Cubbi的解决方案是正确的。
A possibility is to create a new clone_ptr
type for this. 可能是为此创建一个新的
clone_ptr
类型。
Below is a rudimentary example of a clone_ptr
that invokes the correct copy constructor (and destructor) of a derived object. 下面是
clone_ptr
一个基本示例,它调用派生对象的正确复制构造函数(和析构函数)。 This is done here by creating a "type erasure" helper when the clone_ptr
is created. 这是通过在创建
clone_ptr
时创建“类型擦除”帮助clone_ptr
来完成的。
Other implementations may be found on the 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);
}
};
See usage example: http://ideone.com/LnWa3 请参阅用法示例: http : //ideone.com/LnWa3
(But perhaps you don't really need to copy your objects, and might rather explore the possibilities of move semantics. For example, you can have a vector<unique_ptr<T>>
, as long as you don't use functions that copy the contents.) (但也许你真的不需要复制你的对象,而是宁愿探索移动语义的可能性。例如,你可以有一个
vector<unique_ptr<T>>
,只要你不使用复制的函数内容。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.