C++ - unique_ptr 的转换/更改类型

Rip*_*ide 5 c++ types pointers unique-ptr

编辑:谢谢,一如既往的惊人帮助:)

我找不到解决方案,我有一个基类的 unique_ptr,它具有派生类的数据,并且我想将其类型设置为派生类,以便我可以访问派生成员。看这段代码:

#include <memory>
#include <iostream>

class Base
{
public:
    int v1 = 0xAAAAAAAA;
};

class Derived : public Base
{
public:
    int v2 = 0xBBBBBBBB;
};

int main()
{

    std::unique_ptr<Base> b1(new Derived); //How to access b1->v2 ?
    std::unique_ptr<Base> b2(new Base);


    std::getchar(); 
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

b1 的类型是 Base,但它的数据包含 Derived 的数据。看:

问题的屏幕截图。 我们可以看到Derived对应的数据字节

很难得到吗?我想过操纵内存字节,比如说 [b1+4] (伪思想)并访问它,但我想到了复杂的对象,因为我正在为游戏做一个实体系统,我不能这样做:(

谢谢 !

Luk*_*řík 5

您的选择是:

1) 强制转换类似C的指针(推荐)

std::unique_ptr<Base> b2(new Base);
Derived * p = static_cast<Derived *>(b2.get());
Run Code Online (Sandbox Code Playgroud)

2)static_unique_ptr_cast在删除b1并创建新的位置实现您自己的std::unique_ptr

template<typename Derived, typename Base, typename Del>
std::unique_ptr<Derived, Del> 
static_unique_ptr_cast(std::unique_ptr<Base, Del> && p)
{
    auto d = static_cast<Derived *>(p.release());
    return std::unique_ptr<Derived, Del>(d, std::move(p.get_deleter()));
}
Run Code Online (Sandbox Code Playgroud)

该函数采用右值引用来确保您不会从左值窃取资源。用法:

std::unique_ptr<Base> b1(new Derived);
std::unique_ptr<Derived> p = static_unique_ptr_cast<Derived>(std::move(b1));
Run Code Online (Sandbox Code Playgroud)

注意:如果您认为需要使用 2) 我会认为您的设计有缺陷。由于某种原因,演员阵容并未出现在 STL 中。

编辑:现在static_unique_ptr_cast保留删除器。