devxlogo

Using the Template Argument as the Superclass

Using the Template Argument as the Superclass

When you need to add the same member function to several classes, you can use a template class, derived from its template argument.

 template class T : public P {public:  void f() { cout  APlus;typedef T BPlus;

Added in such a manner, this member function calls any other member functions (not defined in the template). However, you have to be sure that all the base classes have them.
This technique can also be used to override a virtual member function of a bunch of classes.

devx-admin

Share the Post: