2016-03-06 85 views
1

什麼叫從一個類繼承與相同的方法名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 
    } 
} 

有沒有辦法做到這一點? 感謝

+2

如果你想打電話給三個不同的功能,你*有*告訴編譯器你想調用哪三個函數。 –

+1

一種選擇是所有基類從事件派發幾乎繼承,並註冊自己的實現施工過程中,調度員。然後,派生最多的類可以通過調度器調用函數列表。 –

回答

3

這是不可能的一個電話讓所有的三個成員函數一次。想象一下,這些成員函數將返回的東西比其他無效:其返回值,你會期待什麼呢?!

如果你想調用所有三個基類的on1msTimer(),你需要顯式調用這些:

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

Online demo