Dro*_*ped 17 c++ inheritance templates operators
我想继承模板类并在调用运算符"()"时更改行为 - 我想调用另一个函数.这段代码
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);
}
};
Run Code Online (Sandbox Code Playgroud)
给我这个错误:
Error 1 error C2955: 'InsertItem' : use of class template requires template argument list...
Run Code Online (Sandbox Code Playgroud)
我想问你如何正确地做到这一点,或者是否有另一种方法可以做到这一点.谢谢.
Rud*_*lis 27
继承时必须显示如何实例化父模板,如果可以使用相同的模板类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);
}
};
Run Code Online (Sandbox Code Playgroud)
如果需要其他东西,只需改变线:
class InsertItem2 : InsertItem<needed template type here>
Run Code Online (Sandbox Code Playgroud)