从继承的类调用匹配方法

时间:2016-03-06 22:00:32

标签: c++ templates inheritance methods multiple-inheritance

从单个类调用匹配方法的最佳方法是什么,该类继承了具有相同方法名称的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
    }
}

有没有办法做到这一点? 感谢

1 个答案:

答案 0 :(得分:3)

一次调用无法同时获取所有三个成员函数。想象一下,这些成员函数会返回除void之外的其他内容:您期望返回的值是多少?

如果要调用所有三个基类的on1msTimer(),则需要明确调用它们:

void onTimer()
{
     fooBase<float>::on1msTimer();
     fooBase<uint8_t>::on1msTimer();
     fooBase<uint16_t>::on1msTimer();
}

Online demo