呼叫从继承类
问题描述:
什么叫从一个类继承与相同的方法名3个基类匹配方法的最佳方法相匹配的方法呢?我想打电话从一个单一的调用这些方法,不知道它甚至有可能呼叫从继承类
template<typename T>
class fooBase()
{
void on1msTimer();
/* other methods that make usage of the template */
}
class foo
: public fooBase<uint8_t>
, public fooBase<uint16_t>
, public fooBase<float>
{
void onTimer()
{
// here i want to call the on1msTimer() method from each base class inherited
// but preferably without explicitly calling on1msTimer method for each base class
}
}
有没有办法做到这一点? 感谢
答
这是不可能的一个电话让所有的三个成员函数一次。想象一下,这些成员函数将返回的东西比其他无效:其返回值,你会期待什么呢?!
如果你想调用所有三个基类的on1msTimer()
,你需要显式调用这些:
void onTimer()
{
fooBase<float>::on1msTimer();
fooBase<uint8_t>::on1msTimer();
fooBase<uint16_t>::on1msTimer();
}
如果你想打电话给三个不同的功能,你*有*告诉编译器你想调用哪三个函数。 –
一种选择是所有基类从事件派发几乎继承,并注册自己的实现施工过程中,调度员。然后,派生最多的类可以通过调度器调用函数列表。 –