“向下转换” unique_ptr <Base>到unique_ptr <Derived>

我有一系列退货的工厂unique_ptr<Base>。不过,在后台,它们提供了指向各种派生类型unique_ptr<Derived>(例如unique_ptr<DerivedA>,unique_ptr<DerivedB>等)的指针。


给定DerivedA : Derived,Derived : Base我们将有:


unique_ptr<Base> DerivedAFactory() {

    return unique_ptr<Base>(new DerivedA);

}

我需要做的是将返回的指针“投射” unique_ptr<Base>到某个派生级别(不一定是原始的内部级别)。为了说明伪代码:


unique_ptr<Derived> ptr = static_cast<unique_ptr<Derived>>(DerivedAFactory());

我正在考虑通过从中释放对象来实现此目的unique_ptr,然后使用一个转换原始指针并将其重新分配给其他unique_ptr所需样式的release函数(在调用之前,调用方将显式完成此操作):


unique_ptr<Derived> CastToDerived(Base* obj) {

    return unique_ptr<Derived>(static_cast<Derived*>(obj));

}

这是否有效,或者是否会发生一些时髦的事情?


PS。更为复杂的是,一些工厂驻留在运行时动态加载的DLL中,这意味着我需要确保在与创建对象相同的上下文(堆空间)中销毁所生成的对象。所有权转移(通常发生在另一个上下文中)必须随后提供原始上下文中的删除器。但是除了必须提供/强制释放删除器以及指针之外,强制转换问题应该相同。


MYYA
浏览 728回答 3
3回答

HUWWW

我将创建几个功能模板,static_unique_ptr_cast和dynamic_unique_ptr_cast。如果您完全确定指针实际上是a Derived *,请使用前者,否则请使用后者。template<typename Derived, typename Base, typename Del>std::unique_ptr<Derived, Del>&nbsp;static_unique_ptr_cast( std::unique_ptr<Base, Del>&& p ){&nbsp; &nbsp; auto d = static_cast<Derived *>(p.release());&nbsp; &nbsp; return std::unique_ptr<Derived, Del>(d, std::move(p.get_deleter()));}template<typename Derived, typename Base, typename Del>std::unique_ptr<Derived, Del>&nbsp;dynamic_unique_ptr_cast( std::unique_ptr<Base, Del>&& p ){&nbsp; &nbsp; if(Derived *result = dynamic_cast<Derived *>(p.get())) {&nbsp; &nbsp; &nbsp; &nbsp; p.release();&nbsp; &nbsp; &nbsp; &nbsp; return std::unique_ptr<Derived, Del>(result, std::move(p.get_deleter()));&nbsp; &nbsp; }&nbsp; &nbsp; return std::unique_ptr<Derived, Del>(nullptr, p.get_deleter());}该功能正在服用右值引用,以确保你没有从主叫方的脚下拉了地毯偷的unique_ptr传递给你。

喵喔喔

您需要从源对象中提取删除程序,并将其注入到目标对象中。该类型很可能是不够的。
打开App,查看更多内容
随时随地看视频慕课网APP