Hi all,
I am somewhat new to templates, but slowly realizing it is very powerful.
Facing a problem. I have a template class.
template <typename T>
class myTemplateClass
{
//Some code here
};
In another class I need to create a fixed number of Objects of the above template class at run time. So what I did is
class myTemplateClassFactory
{
private:
myTemplateClass * templateClassPtrArray[10];
public:
void CreateTemplateClassObject(int index, int type)
{
// Create an int Object
if (type == 1)
{
templateClassPtrArray[index] = new myTemplateClass <int>();
}
// Create a char Object
if (type == 2)
{
templateClassPtrArray[index] = new myTemplateClass <char>();
}
// A large number of other types are there.
.
.
.
.
}
};
This code will not compile because I am trying to assign a myTemplateClass <int> pointer or myTemplateClass <char> pointer to myTemplateClass pointer.
How many objects of each type (int or char), I need to create, is known only at run time, say it can be 1 int and 9 char, 5 int and 5 char etc. But the total number of Object is known i.e 10.
Can we declare a pointer array that can hold myTemplateClass pointer of any type?
Using void pointer will not help much, since it needs a lot of type casting that may introduce bugs. Also it calls for type tracking of each object, which is nasty.
Thanks in advance,
Prem