【发布时间】:2021-05-10 15:48:59
【问题描述】:
我们使用 EASTL,我不能使用 std::static_pointer_cast。
我在我的函数中收到一个指向基类的指针,但不知道如何正确转换它:
switch (command.code)
{
..
case CommandCode::firstCase:
firstCaseFunction(std::shared_ptr<firstCaseType>(static_cast<firstCaseType*>(command.context.get())));
break;
case CommandCode::secondCase:
secondCaseFunction(std::shared_ptr<secondCaseType>(static_cast<secondCaseType*>(command.context.get())));
break;
..
default:
break;
}
上面的代码可以编译,但是在firstCaseFunction/secondCaseFunction的末尾抛出了一些异常(我没有看到异常,可能是因为我们的代码甚至不支持异常)。
代码看起来不正确,但我找不到解决这个问题的正确方法,我尝试了很多版本,但都没有奏效。
我认为强制转换的智能指针对象的生命周期存在问题。
如何让它发挥作用?
【问题讨论】:
标签: c++ shared-ptr smart-pointers downcast