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

Hi!

I would create a base class, from which the myTemplateClass will inherit.

like this:

class TemplateClassBase{};

template<class C>class myTemplateClass : TemplateClassBase{};

This should be a list:

myTemplateClass * templateClassPtrArray[10];

This is better:

std::list<TemplateClassBase*> templateClassPtrArray;
templateClassPtrArray.push_back( new myTemplateClass<int>() );
templateClassPtrArray.push_back( new myTemplateClass<char>() );

Code is NOT tested, was written from scratch.

When you have no problem to save anything.

I hope i was clear :/

Edited 7 Years Ago by programmersbook: n/a

This code will not compile because I am trying to assign a myTemplateClass <int> pointer or myTemplateClass <char> pointer to myTemplateClass pointer.


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.

The only way I can think of to be able to store untyped or generic pointer to either template instance would be to have the template class inherit from a base class the defines all of the operations that you might need to perform on the instances of the template. You could then declare your array to be an array of pointers to the base class and any template instance would satisfy the pointer.

Another approach I can think of is to declare two arrays of pointers, one to myTemplateClass<int> and one to myTemplateClass<char> , both of size 10. If you pre-initialized both arrays to NULL values, you could look at either one first, if the value is NULL, the data item is of the other type.

A third approach might be to have a structure that contains the data type and a union of pointers to the possible implementations. You could then have an array of those.

Both of the last two cases require you to keep re-evaluating the type and handling the pointers in a type-safe fashion.

You might be able to simplify handling a little if there were rules to the int/char transition...for example if the characters were always first and after the switch to integers, there were no more characters. But if the rules are that there are no rules, you're stuck with the testing unless you can define generic behaviors.

I'm not sure which implementation might be the best, you've given no clue as to what you have to be able to do with them later and what the template might be doing with or for them, so GIGO may apply here.

Hope this helped.

I think creating the concrete class is [thanks to programmers book and murtan for the idea] the best and straight forward method. But facing another small problem. If I want to specify that my template derived class should contain a function (say function1()), I can make it as pure virtual in the concrete base class TemplateClassBase. But the problem is that function1() has abstract arguments. So I don't know how to declare it as pure virtual in the base class. Any ideas?

Edited 6 Years Ago by Premlal: n/a

The compiler requires that the 'interface' function1() declared in the base class have a concrete argument list.

What kind of abstraction do you think it needs?

Could that abstraction be implemented with another base class?

If the function call is abstract, how will the caller know what to pass the abstraction?

I am trying to declare function1 as pure virtual in the concrete base class to ensure that all the derived classes must implement this function1(). But in the derived template class we don't have the concrete arguments (say, no int function1(int) or char function1(char) instead "typename X function1(typename X)"). Since the typename X is only visible or valid for the template class, I don't know how to declare this as a pure virtual function in the base class. If it was "int function1(int)" in the derived template class I can declare the function in the base class like "int function1(int) = 0;". But in case of "typename X function1(typename X)" I dont know how to declare this function as pure virtual in the concrete base class. May be my design is bad... :(

There may be design issues here...

The base class can declare any function it likes as pure virtual, but all of the child classes must implement it using the same concrete argument list.

If your generic list contained instances derived from the base class, how would someone using the list have any idea about which kind of object it was? (I thought the idea was to make it so users of the list could treat them all the same.)

If you need different behavior (function calls) then you need different types and the caller needs to know what kind it is. (You're back to looking at the type of each item and handling it appropriately.)

This question has already been answered. Start a new discussion instead.