我在头中声明了两个c++类。基类声明一个虚拟方法,第二个类重写它。实现在.cpp文件中。
代码相当简单
void DefendProperty::apply(Queue<Defend*>* defendQueue,
const Tool* toolSource, const Actor* actorSource, const Actor* defender) {
cout << "BASE" << endl;
}void DefendPropertyPhysical::apply(Queue<Defend*>* defendQueue,
Tool* toolSource, const Actor* actorSource, const Actor* defender) {
cout << "CORRECT" << endl;
defendQueue->enqueue(new Defend(
DefendTypePhysical::TYPE,
new DamageValuesPhysical(
getRandomDouble(minDamageReduction, maxDamageReduction))
));
}问题是,当我调用实例化为B的类时,它输出基,而不是正确的。我不知道这是怎么回事。
这些类存储在没有apply方法的基本ToolProperty类型中。当它们被调用时,会使用DefendProperty将它们输入到dynamic_cast类型中。
dynamic_cast<DamageProperty*>(node->value)->apply(damageQueue, toolSource, actorSource);如能提供任何帮助,将不胜感激。
发布于 2012-05-07 18:43:01
你的功能有不同的签名。查看"toolSource“的类型。第二个不是第一个的覆盖,而是一个过载。
编译器几乎永远不会警告您的常见错误。我不知道有谁能做到。
顺便说一句,如果要在指针上使用动态强制转换而不检查结果,就没有理由使用它。
https://stackoverflow.com/questions/10487260
复制相似问题