子功能还有就是我的一个简单的代码版本:调用从父常量对象
class Action:
{
int updateOK() {
std::cout << "" << std::endl;
return 0;
}
}
class Attack:public Action
{
int updateOK() {
std::cout << "att" << std::endl;
return 0;
}
}
class Def:public Action
{
int updateOK() {
std::cout << "DEf" << std::endl;
return 0;
}
}
namespace actions
{
const Attack light = Attack();
const Attack meduim = Attack();
const Attack heavy = Attack();
const Block block = Block();
const Block cancelBlock = Block();
const std::vector<Action> listAction = { light , meduim, heavy , block, cancelBlock };
}
Main.cpp :
for each (Action action in actions::listAction) {
action.updateOK();
}
的问题是,它总是调用主父功能。
我已经尝试了许多方式的虚拟功能,但我想找到解决方案的问题没有强制转换。
可能吗?
您需要重新[多态性是如何工作的(http://www.tutorialspoint.com/cplusplus/cpp_polymorphism.htm) – NathanOliver
提示。关键字覆盖是你最好的朋友在这里:-) – Thinkeye
你是什么意思没有演员?在父功能之前添加虚拟似乎是没有重大改变的方式。 – aybassiouny