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 << "f()" << endl; } _// added member function};typedef T 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.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


Recent Articles: