Class template inheritance C++

后端 未结 1 349
夕颜
夕颜 2020-12-14 17:39

I\'d like to inherit from the template class and change the behavior when the operators \"()\" are called - I want to call another function. This code

templa         


        
相关标签:
1条回答
  • 2020-12-14 18:23

    When inheriting you must show how to instantiate the parent template, if same template class T can be used do this:

    template<typename T>
    class InsertItem
    {
    protected:
        int counter;
        T   destination; 
    
    public:
        virtual void operator()(std::string item) {
            destination->Insert(item.c_str(), counter++);
        }
    
    public:
        InsertItem(T argDestination) {
            counter= 0;
            destination = argDestination;
        }
    };
    
    template<typename T>
    class InsertItem2 : InsertItem<T>
    {
    public:
        virtual void operator()(std::string item) {
            destination ->Insert2(item.c_str(), counter++, 0);
        }
    };
    

    If something else is needed just change the line:

    class InsertItem2 : InsertItem<needed template type here>
    
    0 讨论(0)
提交回复
热议问题