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.
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.
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.
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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With