将auto_ptr转换为无效指针

Casting auto_ptr to void pointer

本文关键字:无效 指针 转换 ptr auto      更新时间:2023-10-16

我正在尝试通过以下方式将auto_ptr转换为无效指针:

void *AM::This2Ctx(std::auto_ptr<AMContext> data)
{
 return reinterpret_cast<void *>(data);
}

但是我不断收到编译错误:

error: invalid cast from type std::auto_ptr<AMContext> to type void*

如何正确完成这种铸造?以及如何以相反的方式使用它?

使用 .get() 访问 auto-ptr 持有的指针:

reinterpret_cast<void *>(data.get());
                             ~~~~~~

此外,auto_ptr已弃用,请改用unique_ptr