Template Base Class with Factories
Hi, I'm going to use a simplified example to ask my question.
Suppose I have a base class:
template <class T> class BaseClass {
.....
};
with a couple of classes which inherit it
class Example1 : public BaseClass<int> {
.....
};
class Example2 : public BaseClass<float> {
.....
};
Question: How do I write a Factory class to return a reference to
BaseClass? Such as:
class FactoryClass {
public:
template <class T> BaseClass<T> *Create(int option) {
switch (option) {
case 0 : return(new Example1);
case 1: return(new Example2);
};
}
};
Where in my code I would call it as such.....
FactoryClass thisFactory;
BaseClass<???> *thisBaseClass1 = thisFactory.Create(0);
BaseClass<???> *thisBaseClass2 = thisFactory.Create(1);
Now, I know that BaseClass is defined with a template parameter, but
in this case I do not know what that parameter is until the factory
creates it. Is there a way to define the local reference to BaseClass
in the code such that "I don't know what the type is and I don't care
at this point"? Go easy on me, I'm relatively new to templates.
Thanks!
D