How to force template to be derived from BaseClassA?

前端 未结 3 971
暗喜
暗喜 2020-12-15 20:55

Is there any possibility to force a template to be from a certain base class so that I can call the base class function?

template 
void SomeMan         


        
3条回答
  •  旧时难觅i
    2020-12-15 21:47

    The easiest solution is to add a snippet of code that compiles only if it's what you expected:

    template 
    void SomeManager::Add(T t)
    {
        assert((Base const*)&t); // T must inherit from Base to allow T*->Base* conversion.
        t.CallTsBaseClassFunction();
        //... do other stuff
    }
    

提交回复
热议问题