【发布时间】:2023-03-08 17:05:01
【问题描述】:
我需要复制一些接口Interface的std::unique_ptr<Interface>。
This post 总结得很好,但在我的情况下它不起作用,因为Interface 没有可用的构造函数。
例子:
//Pointer to copy
std::unique_ptr<Interface> ptr = std::make_unique<Interface>();
//error: incomplete type is not allowed
std::unique_ptr<Interface> copy{ new Interface(*ptr.get().data) };
有没有办法将ptr 深度复制到copy?
【问题讨论】:
-
Interface的定义是什么? -
找出答案。如果不知道您的代码的作用,我们将无法帮助您。
-
如果
Interface支持复制,您可以为复制的对象创建一个新的unique_ptr。否则没有副本.... -
如果您的类不可复制,将其隐藏在 unique_ptr 后面不会突然神奇地使其可复制。
-
投票关闭为不清楚,因为它真的不清楚。特别是没有指定
Interface,也没有指定代码所有权。
标签: c++ c++11 unique-ptr