Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class template inheritance C++

Tags:

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

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

gives me this error:

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

I'd like to ask you how to do this properly, or if there is another way to do this. Thanks.

like image 586
DropDropped Avatar asked Oct 15 '12 12:10

DropDropped


People also ask

Can template class be inherited?

Inheriting from a template classIt is possible to inherit from a template class. All the usual rules for inheritance and polymorphism apply. If we want the new, derived class to be generic it should also be a template class; and pass its template parameter along to the base class.

Is inheritance better than templates?

Templates provide sortability in a much nicer way, since the sortee doesn't need to know that it's being sorted. Complex inheritance typically results when you work with a forced mindset that everything must be an inheritance hierarchy, which is in fact rarely appropriate.

What is a class template?

Class templates A class template provides a specification for generating classes based on parameters. Class templates are generally used to implement containers. A class template is instantiated by passing a given set of types to it as template arguments.

What is the syntax inheritance of class?

Which is the correct syntax of inheritance? Explanation: Firstly, keyword class should come, followed by the derived class name. Colon is must followed by access in which base class has to be derived, followed by the base class name.


1 Answers

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> 
like image 159
Rudolfs Bundulis Avatar answered Oct 02 '22 08:10

Rudolfs Bundulis