从继承的类调用匹配方法

Call matching methods from inherited classes

本文关键字:方法 调用 继承      更新时间:2023-10-16

从继承具有相同方法名的其他 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
    }
}

有什么办法可以做到这一点吗?谢谢

一次调用不可能同时获取所有三个成员函数。想象一下,这些成员函数将返回 void 以外的其他内容:您希望哪个返回值?!

如果要调用所有三个基类的on1msTimer(),则需要显式调用这些基类:

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

在线演示