PDA

View Full Version : Dynamic template



seth___
9th September 2008, 16:16
Hi

I have a problem that depends on template class. I have declaration of class, for example:

template<typename T> class A;

And I want to create a variable, but I want to decide later what will be template argument (user will be able to select between int, float etc). I can't find good solution, i tried pointer:

A *instance;

and later: instance = new A<float>(); but there is compiler error in declaration(missing template argument). I also tried void pointer, it works with creating objects, but later I'm unable to call any method from class A.

If you have some idea please help me :)

lyuts
9th September 2008, 16:28
Show us your implementatin of A.

seth___
9th September 2008, 16:51
The problem don't depends on implementation of class (unless there is some design pattern that may be usefull), you can have the simplest example of class with template argument. The qestion is: how to create an object, and decide later what will be the tempalte argument

wysota
10th September 2008, 00:33
Use inheritance.

class Base {
//...
};

template<typename T> class A : public Base {
//...
};

Base *var;
var = new A<int>;

seth___
11th September 2008, 11:15
Thanks, thats it.