C++ Combining Static and Dynamic Polymorphism to Create "Hyper Polymorphism"?

c++, inheritance, polymorphism, templates

Solution

From reading the comments it looks like this technique is actually considered a "decorator pattern".

It can be used when you need to extend the functionality of multiple existing classes (for example adding a member variable `z`) when you can't modify the classes directly.

It's more flexible than regular inheritance because you can extend the functionality of many classes by only writing one new class.

Not exactly the same as "hyper polymorphism", but hey, maybe that will be a thing some day. ;)

Problem

By mixing both static and dynamic polymorphism (templates and inheritance) I have come across a strange technique that functions similarly to regular static polymorphism in C++, except the members of the child class are still visible after creating the new object. Consider the following example: Base.h: ``` #include <iostream> class Base { public: virtual ~Base() {} virtual void say_hello() { std::cout << "Hello from Base!" << std::endl; } }; ``` Class1.h: ``` #include "Base.h" #include <iostream> class Class1 : public Base { public: virtual void say_hello() { std::cout << "Hello from Class1!" << std::endl; } int x = 1; }; ``` Class2.h: ``` #include "Base.h" #include <iostream> class Class2 : public Base { public: virtual void say_hello() { std::cout << "Hello from Class2!" << std::endl; } int y = 2; }; ``` This is where things get interesting... ClassX.h ``` template <class T> class ClassX : public T { public: int z = 3; }; ``` By implementing classX in such a way that it can dynamically inherit from anything it allows some strange things to occur. See the example below showing it in use. main.cpp ``` #include <iostream> #include "Base.h" #include "Class1.h" #include "Class2.h" #include "ClassX.h" using namespace std; int main(int argc, char* argv[]) { Base* b = new Base; b->say_hello(); // Regular polymorphism in action Base* c1 = new Class1; c1->say_hello(); // Aware that this is Class1 //cout << c1->x << endl; // Doesn't work! Not visible from here Base* c2 = new Class2; c2->say_hello(); // Aware that this is Class2 //cout << c2->y << endl; // Doesn't work! Not visible from here // Hyper polymorphism!? Not sure what to call this. ClassX<Class1> cx1; cx1.say_hello(); // Aware that this is Class1 cout << cx1.x << endl; // The member variable is visible! cout << cx1.z << endl; // Also available :) ClassX<Class2> cx2; cx2.say_hello(); // Aware that this is Class2 cout << cx2.y << endl; // The member variable is visible! cout << cx2.z << endl; // Also available :) // ALWAYS delete objects created with "new" or shame on yew. delete b; delete c1; delete c2; } ``` What I'm wondering is why have I never seen this technique before? I've never once seen anyone try to inherit from an unknown class using templates like this: ``` template <class T> class Name : public T { // Implementation }; ``` Is there a name to this technique, and what are it's uses? I just gave it a try because knowing the rules of C++ I didn't see a reason why it wouldn't work. Since I can't seem to find a name for it anywhere I'm going to call this technique "Hyper Polymorphism" :)

Original source