Наследование шаблона класса С++ - программирование
Подтвердить что ты не робот

Наследование шаблона класса С++

Я хотел бы наследовать из класса шаблона и изменять поведение при вызове операторов "()" - я хочу вызвать другую функцию. Этот код

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
{
public:
 virtual void operator()(std::string item) {
  destination ->Insert2(item.c_str(), counter++, 0);
 }
};

дает мне эту ошибку:

Error 1 error C2955: 'InsertItem' : use of class template requires template argument list...

Я хотел бы спросить вас, как это сделать правильно, или если есть другой способ сделать это. Спасибо.

4b9b3361

Ответ 1

При наследовании вы должны показать, как создать экземпляр родительского шаблона, если тот же шаблонный класс T может быть использован:

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);
    }
};

Если нужно что-то еще, просто измените строку:

class InsertItem2 : InsertItem<needed template type here>