templates may not be ‘virtual’

后端 未结 4 954
清酒与你
清酒与你 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条回答
  •  北荒
    北荒 (楼主)
    2021-02-18 19:34

    You can use what we call in Symbian as "template design pattern". Here is sample code to give you an idea:

    class Base {
    public:
            virtual int DoSomething() = 0;
    protected:
            Base();
    };
    
    class IntermediateBase : public Base {
    protected:
            IntermediateBase(void* aSomeParam, void* aArg)
            : iSomeParam(aSomeParam)
            , iArgs(aArg) 
            {}
    
            virtual int DoSomething() = 0;
    protected:
            void* iSomeParam;
            void* iArgs;
    };
    
    template 
    class ConcreteClass : public IntermediateBase {
            typedef int (TYPE::*MemberFuncPtr)(const INPUT&);
    public:
            ConcreteClass(TYPE& aCommandType, 
                          INPUT& aArgumentsToCommand,
                          MemberFuncPtr aMFP)
            : IntermediateBase(static_cast(&aCommandType),
                               static_cast(&aArgumentsToCommand) )
            , iMFP(aMFP)
            {}
    
            virtual int DoSomething()  // VIRTUAL AND INLINE Note - dont make it 
                                       // virtual and inline in production if 
                                       // possible to avoid out-of-line copy   
            {
                return static_cast(iSomeParam)->*ConcreteClass::iMFP)
                               (*(static_cast(iArgs));
            }
    private:
            MemberFuncPtr iMFP;
    }; 
    

提交回复
热议问题