templates may not be ‘virtual’

后端 未结 4 944
清酒与你
清酒与你 2021-02-18 18:47

Given the code below, the compiler is showing a message pointing that error: templates may not be ‘virtual’. Does anyone have a suggestion on how to solve the bug?<

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-18 19:43

    If you really need to make this method virtual, consider making CBar<> polymorphic and pass a base type in which isn't templated.

    EDIT: something like this:

    // non-templated base class
    class BarBase
    {
     // common methods go here..
    };
    
    template 
    class CBar : public BarBase
    {
     // implement methods from BarBase ...
    };
    
    template < class FOO_TYPE>
    class CFoo{
        public:
            ...
            // now we take the base type, and this method does not need to be a template
            virtual void doSomething( BarBase const* ptrBar );
            ...
            virtual ~CFoo();
        protected:
            MyClass < FOO_TYPE > * m_pClass;
    };
    
    template < class FOO_TYPE >
    void CFoo::doSomething( BarBase const* ptrBar ){
    ..
    }
    

提交回复
热议问题