[英]deep copy for C++ boost::shared_ptr
I am trying to do deep copy for C++ boost::shared_ptr. 我正在尝试为C ++ boost :: shared_ptr做深层复制。
struct A{
boost::shared_ptr<const Data> dataPtr;
A(const A& aSource) {
dataPtr.reset(new const Data);
*dataPtr = *(aSource.dataPtr);
};
But, i got error: error: uninitialized const in 'new' of 'const struct A. 但是,我得到了一个错误:错误:'const struct A的'new'中的未初始化const。
If I do not want to drop const, how to handle that ? 如果我不想删除const,该如何处理?
Any help will be appreciated. 任何帮助将不胜感激。
Thanks ! 谢谢 !
That is because you are trying to modify (in particular, to assign) a value through a const
reference to it (because this is what dereferencing a shared_ptr
to const
gives you). 那是因为您试图通过const
引用来修改(特别是赋值)一个值(因为这是将shared_ptr
取消引用为const
会给您的结果)。 Supposing Data
has a copy constructor, you should rewrite your program this way: 假设Data
具有一个复制构造函数,则应采用以下方式重写程序:
struct A
{
boost::shared_ptr<const Data> dataPtr;
A(A const& aSource)
{
dataPtr.reset(new Data(*(aSource.dataPtr)));
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.