原因:
contains a std::unique_ptr.
A std::unique_ptr cannot be copied
or
contains a std::shared_ptr
A std::unique_ptr cannot be copied
解决方法:
传入:
//cpp1
class B{
A::getPTR(target_ptr)
}
//cpp2
class A{
getPTR(const B& target_ptr)
{
std::shared_ptr<B> copied_ptr = std::make_shared<B>(target_ptr);
}
}
target_ptr : is the ptr you wanna copy
copied_ptr : is the ptr you copied
本文探讨了std::unique_ptr不能被复制的原因,并提供了一种通过std::shared_ptr来实现对象复制的方法。对于需要传递智能指针的场景,使用这种方式可以有效避免编译错误。
1464

被折叠的 条评论
为什么被折叠?



