can I mix inheritance and templates this way ? :
template <class T>
class AbstractType { // abstract
//....
}
template <class T>
class Type1 : public AbstractType<T> {
//....
}
And later on, can I use these classes like this:
AbstractType<SomeClass>* var1 = new Type1<SomeClass>();
Thx for help.
You can, but it’s not going to be as useful as you may think. You can define the structures like this:
But when you’re defining abstract base types, you’re often going to want a collection of them, and to be able to call through a base class pointer to get polymorphic behavior. If you templatize the base class, you’re not going to be able to do this because each variation of the template parameters will create a different type.
Base<int>is completely different thanBase<string>, and you can’t just get aBase*that points to either one.This code will not compile: