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 << "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.

See also  How to Create and Deploy QR Codes Online: A Comprehensive Guide
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist