Virtual Classes gives you at runtime what templates give you at compile time .. kind of.
Template code when written is not real code, just a template that can be used to generate code. Every time you use it .. it'll usually generate the code right there. eg.
Z<int, std::string> z; // At this point a new class called Z<int, std::string> is defined, then the code to instantiate it at run time is generated.
This might be a useful read: c++ standard practice: virtual interface classes vs. templates
Maybe this is what you're after ? The similar sort of thing using polymorphism and virtual classes ..
class Z{
public:
virtual void doSomething();
};
class ZT : public Z {
public:
void doSomething();
};
class ZA : public Z {
public:
void doSomething();
};
...
void useClasses(Z* ptr) {
ZT* ztPtr = dynamic_cast<ZT*>(ptr); // Runtime conversion
if (ztPtr) {
// do specific stuff for this type
} else {
ptr->doSomething(); // generic behaviour that'll call whichever class ptr actually is
}
}