我想检查我要处理的对象,因为我想替换它们,所以我进行了强制转换。
void Promote(vector<shared_ptr<Employee>>& sourceEmployee) {
auto it = std::find_if(sourceEmployee.begin(), sourceEmployee.end(),
[&sourceEmployee, id](const auto &obj) { return obj->getID() == id; });
if (it != sourceEmployee.end()) {
// I want to return true whether I'm dealing with "Intern" class object
if (auto w = dynamic_cast<std::shared_ptr<Intern>>(it->get())) { // ERROR
auto tmp0 = std::move(*it);
*it = std::make_shared<Worker>(*tmp0);
}
此行出现错误(目标不是指针或引用):
if (auto w = dynamic_cast<std::shared_ptr<Intern>>(it->get()))
编辑:我很傻。只需使用auto w = dynamic_cast<Intern*>(it->get())